实验4 函数与异常处理应用编程


print
(sum) sum = 42 print(sum) def inc(n): sum = n+1 print(sum) return sum sum = inc(7) + inc(7) print(sum)
#不是;line1、3、11是全局作用域。line7是函数作用域。
Task2

 

list1 = [1, 9, 8, 4]
print( sorted(list1) )
print( sorted(list1, reverse=True) )
print( sorted(list1, True) )

def func(a, b, c, /, *, d, e, f):
    return( [a,b,c,d,e,f] )
print(func(1,2,3,d=4,e=5,f=6))

Task3

def solve(a, b, c):
    delta = b*b - 4*a*c
    delta_sqrt = abs(delta)**0.5
    p1 = -b/2/a;
    p2 = delta_sqrt/2/a
    print(solve.__doc__)

    if delta>=0:
        root1 = p1 + p2
        root2 = p1 - p2
    else:
        root1 = complex(p1, p2)
        root2 = complex(p1, -p2)
        return root1, root2
while True:
    try:
        a,b,c = eval(input('Enter eqution coefficient: '))
        if a == 0:
            raise
    except:
        print('invalid input, or, a is zero')
        break
    else:
        root1, root2 = solve(a, b, c)
        print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
        print()

Task4

def list_generator(a, b, c = 1):
    list = []
    n = a
    while(n <= 5):
        list.append(n)
        n = n + c
    return list
list1 = list_generator(-5, 5)
print(list1)
list2 = list_generator(-5, 5, 2)
print(list2)
list3 = list_generator(1, 5, 0.5)
print(list3)

Task

def is_prime(n):
    if n==1:
        return False
    elif n==2:
        return True
    else:
        for i in range(2,n):
            if n%i==0:
                return False
                break
            else:
                return True
sushu=[]
for i in range(1,21):
    if is_prime(i)==True:
        sushu.append(i)
oushu=[]
for i in range(1,21):
    if i%2==0:
        oushu.append(i)
for n in oushu:
    for i in range(len(sushu)):
        for j in range(len(sushu)):
            if(n == sushu[i] + sushu[j]):
                print('{} = {} + {}'.format(n, sushu[i], sushu[j]))
                break
        if(n == sushu[i] + sushu[j]):
            break

Task6

def encoder(x):
    x = list(x)
    for i in range(len(x)):
        if('z' >= x[i].lower() >= 'v'):
            x[i] = chr(ord(x[i]) - 21)
        elif('v' > x[i].lower() >= 'a'):
            x[i] = chr(ord(x[i]) + 5)
    x = ''.join(x)
    return x
def decoder(x):
    x = list(x)
    for i in range(len(x)):
        if('a' <= x[i].lower() <= 'e'):
            x[i] = chr(ord(x[i]) + 21)
        elif('e' < x[i].lower() <= 'z'):
            x[i] = chr(ord(x[i]) - 5)
    x = ''.join(x)
    return x
wenben = input('输入英文文本;')
print('编码后的文本:{}'.format(encoder(wenben)))
print('对编码后的文本解码:{}'.format(decoder(encoder(wenben))))

Task7

def collatz(x):
    if(x % 2 == 0):
        x = int(x / 2)
    else:
        x = 3 * x + 1
    return x

list = []

try:
    n = eval(input('Enter a positive integer: '))

    if(n <= 0):
        raise
    elif(int(n) != n):
        raise

    while True:
        if(n == 1):
            list.append(n)
            break
        else:
            list.append(int(n))
            n = collatz(n)
    print(list)
except:
    print('Error: must be a positive integer')

实验总结:学会了学会了

 

posted @ 2022-05-06 18:36  琦哲  阅读(26)  评论(2编辑  收藏  举报