实验四 函数与异常处理

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

 

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):
    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
while True:
    try:
        t=input('输入一元二次方程系数a b c,或者,输入#结束:')
        if t=='#':
            print('结束计算,退出')
            break
        a,b,c=map(float,t.split())
        if a==0:
            raise ValueError('a=0,不是一元二次方程')
    except ValueError as e:
        print(repr(e))
        print()
    except:
        print('有其它错误发生\n')
    else:
        root1,root2=solve(a,b,c)
        print(f'root1={root1:.2f},root2={root2:.2f}')
        print()

 

def list_generator(a, b, c=1):
    if type(c)== int:
        return list(range(a, b+1, c))
    else:
        lst = [a]
        x = a
        while x < b:
            x += c
            lst.append(x)
        return lst
list1 = list_generator(-5, 5)
print(list1)

list2 = list_generator(-5, 5, 2)
print(list2)

list3 = list_generator(1, 5, 0.5)
print(list3)

 

def is_prime(n):
    if n < 2:
        return False
    else:
        for i in range(2, n):
            if n % i == 0:
                return False
        else:
            return True

prime = []
for i in range(2, 21):
    if is_prime(i):
        prime.append(i)
    else:
        continue

for j in range(2, 21, 2):
    for a in prime:
        if (j-a) in prime and a <= (j-a):
            print(f'{j} = {a} + {j-a}')
        else:
            continue

 

def encoder(x1):
    x2 = ''
    for i in x1:
        if i.isalpha():
            if 65 <= ord(i) <= 85 or 97 <= ord(i) <= 117:
                i = chr(ord(i)+5)
                x2 += i
            elif ord(i) > 85 or ord(i) > 117:
                i = chr(ord(i)+5-26)
                x2 += i
        else:
            x2 += i
    return x2


def decoder(y1):
    y2 = ''
    for j in y1:
        if j.isalpha():
            if 65 <= ord(j) <= 69 or 97 <= ord(j) <= 101:
                j = chr(ord(j)+26-5)
                y2 += j
            elif ord(j) > 69 or ord(j) > 101:
                j = chr(ord(j)-5)
                y2 += j
        else:
            y2 += j
    return y2


text = input('输入英文文本: ')

encoded_text = encoder(text)
print('编码后的文本: ', encoded_text)

decoded_text = decoder(encoded_text)
print('对编码后的文本解码: ', decoded_text)

 

def collatz(n):
    lst = [n]
    while lst[-1] != 1:
        if lst[-1] % 2 == 0:
            lst.append(lst[-1]//2)
        else:
            lst.append(lst[-1]*3+1)
    return lst


try:
    n = int(input('Enter a positive integer:'))
    if n <= 0:
        raise
except:
    print('Error: must be a positive integer')
else:
    print(collatz(n))

 

 

def func(n):
    if n == 0:
        result = 1
        for i in range(n):
            result *= 2
        return result-1


while True:
    x = input()
    if x == '#':
        print('计算结束')
        break
    n = int(x)
    ans = func(n)
    print(f'n = {n}, ans = {ans}')

 


 

posted @ 2023-05-19 12:24  曾诚  阅读(10)  评论(0编辑  收藏  举报