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

print(sum)
sum = 42
print(sum)

def inc(n):
    sum = n + 1
    print(sum)
    return sum

sum = inc(7) + inc(7)
print(sum)

不是:

第一个sum作用的是一开始定义的42;

第二个sum作用的是调用的n+1;

第三个sum作用的是调用里的另一个;

第四个sum作用的是最后一个两者相加的。

复制代码
# task2_1.py
def func1(a, b, c, d, e, f):
    return [a, b, c, d, e, f]

def func2(a, b, c, *, d, e, f):
    return [a, b, c, d, e, f]

def func3(a, b, c, /, d, e, f):
    return [a, b, c, d, e, f]

print(func1(1, 9, 2, 0, 5, 3))
print(func1(a=1, b=9, c=2, d=0, e=5, f=3))
print(func1(1, 9, 2, f=3, d=0, e=5))

print(func2(11, 99, 22, d=0, e=55, f=33))
print(func2(a=11, b=99, c=22, d=0, e=55, f=33))

print(func3(111, 999, 222, 0, 555, 333))
print(func3(111, 999, 222, d=0, e=555, f=333))

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))

def solve(a, b, c):
    '''
    求解一元二次方程, 返回方程的两个根
    :para: a,b,c: int 方程系数
    :return: tuple
    '''
    delta = b * b - 4 * a * c
    delta_sqrt = abs(delta) ** 0.5
    p1 = -b / 2 / a
    p2 = delta_sqrt / 2 / a
    if delta >= 0:
        root1 = p1 + p2
        root2 = p1 - p2
    else:
        root1 = complex(p1, p2)
        root2 = complex(p1, -p2)
    return root1, root2
print(solve.__doc__)
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()

def list_generator(begin,end,step=1):
    list=[]
    while begin<=end:
        list.append(begin)
        begin=begin+step

    return list
list1=list_generator(-5,5)
print(list1)
list2=list_generator(-5,5,2)
print(list2)
list3=list_generator(-5,5,0.5)
print(list3)

def is_prime(n):
    for i in range(2,n):
        if n%i==0:
            break
            return False
    else:
        return True
list1=list(i for i in range(4,21,2))
for x in list1:
    list2=[]
    for i in range(2,x):
        if is_prime(i)==True:
            list2.append(i)
    for i in list2:
        if (x-i) in list2:
            print(f'{x}={i}+{x-i}')
            break

def encoder(n):
    list1=[]
    for i in n:
        x=ord(i)

        if 65<=x<=90 or 97<=x<=122:
            if 65<=x+5<=90 or 97<=x+5<=122:
                y=chr(x+5)

                list1.append(y)
            else:

                y=chr(x-21)
                list1.append(y)
        else:


            list1.append(i)
    z="".join(list1)
    return z


n=input('输入英文文本:')
x1=encoder(n)
print(f'编码后的文本:{x1}')
def decoder(n):
    list1=[]
    for i in n:
        x=ord(i)

        if 65<=x<=90 or 97<=x<=122:
            if 65<=x-5<=90 or 97<=x-5<=122:
                y=chr(x-5)

                list1.append(y)
            else:

                y=chr(90-(65-(x-5))+1)
                list1.append(y)
        else:


            list1.append(i)
    z="".join(list1)
    return z
x2=decoder(x1)
print(f'对编码后的文本解码{x2}')

def collatz(x):
    list1=[]
    while x!=1:
        if x%2==0:
            x=x/2
            list1.append(x)
        else:
            x=(3*x)+1
            list1.append(x)

    return list1
try:
    x=int(input('Enter a positive integer:'))
    if x<0:
        print('Error:must be a positive integer')
    else:
        print(collatz(x))
except(ValueError):
    print('Error:must be a positive integer')

 

posted @ 2022-05-10 23:05  李禹灼  阅读(18)  评论(2编辑  收藏  举报