实验四 函数与异常处理编程
实验任务一
task1:
源代码:
1 print(sum) 2 sum = 42 3 print(sum) 4 5 def inc(n): 6 sum = n+1 7 print(sum) 8 return sum 9 10 sum = inc(7) + inc(7) 11 print(sum)
运行结果:
实验任务二
task2-1
源代码:
1 def func1(a,b,c,d,e,f): 2 return [a,b,c,d,e,f] 3 4 def func2(a,b,c,*,d,e,f): 5 return [a,b,c,d,e,f] 6 7 def func3(a,b,c,/,d,e,f): 8 return [a,b,c,d,e,f] 9 10 print(func1(1,9,2,0,5,3)) 11 print(func1(a=1,b=9,c=2,d=0,e=5,f=3)) 12 print(func1(1,9,2,f=3,d=0,e=5)) 13 14 print(func2(11,99,22,d=0,e=55,f=33)) 15 print(func2(a=11,b=99,c=22,d=0,e=55,f=33)) 16 17 print(func3(111,999,222,0,555,333)) 18 print(func3(111,999,222,d=0,e=555,f=333))
运行结果:
line16 增加 print(func2(11,99,22,0,55,33)):
line18 增加 print(func3(a=111, b=999, c=222, 0, 555, 333) )
task2-2
源代码:
1 list1 = [1,9,8,4] 2 3 print(sorted(list1)) 4 print(sorted(list1,reverse = True)) 5 print(sorted(list1,True))
运行结果:
问题回答:sorted内置函数中,reverse必须按照关键字传递。
task2-3
源代码:
1 def func(a,b,c,/,*,d,e,f): 2 return([a,b,c,d,e,f]) 3 4 print(func(1,2,3,d=4,f=5,e=6))
运行结果:
实验任务三
task3
源代码:
1 def solve(a,b,c): 2 ''' 3 求解一元二次方程, 返回方程的两个根 4 5 :para: a,b,c: float 方程系数 6 :return: tuple 7 ''' 8 delta = b*b - 4*a*c 9 delta_sqrt = abs(delta)**0.5 10 p1 = -b/2/a 11 p2 = delta_sqrt/2/a 12 13 if delta > 0: 14 root1 = p1+p2 15 root2 = p1- p2 16 17 else: 18 root1 = complex(p1,p2) 19 root2 = complex(p1,-p2) 20 21 return root1,root2 22 23 while True: 24 try: 25 t = input('输入一元二次方程系数a b c, 或者,输入#结束:') 26 if t == '#': 27 print('结束计算,退出') 28 break 29 a,b,c = map(float,t.split()) 30 if a == 0: 31 raise ValueError('a = 0, 不是一元二次方程') 32 except ValueError as e: 33 print(repr(e)) 34 print() 35 except: 36 print('有其它错误发生\n') 37 else: 38 root1,root2 = solve(a,b,c) 39 print(f'root1 = {root1:.2f},root2 = {root2:.2f}') 40 print()
运行结果:
line23增加 print(solve.__doc__):
实验任务四
task4
源代码:
1 def list_genetator(q,z,b = 1): 2 lst = [] 3 while q <= z: 4 if q <= z+1: 5 lst.append(q) 6 q = q+b 7 else: 8 break 9 10 return lst 11 12 list1 = list_genetator(-5,5) 13 print(list1) 14 15 list2 = list_genetator(-5,5,2) 16 print(list2) 17 18 list3 = list_genetator(1,5,0.5) 19 print(list3)
运行结果:
实验任务五
task5
源代码:
1 def is_prime(n): 2 if n > 1: 3 if n%2 > 0: 4 return True 5 elif n == 2: 6 return True 7 else: 8 return False 9 else: 10 return False 11 12 s = [i for i in range(1,21)] 13 s_1 = [] 14 s_2 = [] 15 s_3 = {} 16 for j in s: 17 if is_prime(j) == True: 18 s_1.append(j) 19 for y in s: 20 if y > 2 and y%2 == 0: 21 s_2.append(y) 22 for i in range(len(s_2)): 23 for z in s_1: 24 for q in s_1: 25 if z + q == s_2[i]: 26 s_3.setdefault(s_2[i],(z,q)) 27 for key,value in s_3.items(): 28 a = value[0] 29 b = value[1] 30 print(f'{key} = {a}+{b}')
运行结果:
实验任务六
task6
源代码:
1 def encoder(s): 2 al = [chr(i) for i in range(97,123)]*2 3 al_m = [chr(i) for i in range(65,91)]*2 4 s_1 = list(s) 5 for j in s_1: 6 if j in al: 7 a = al.index(j) 8 a1 = s_1.index(j) 9 t = al[a+5] 10 s_1[a1] = t 11 elif j in al_m: 12 b = al_m.index(j) 13 b1 = s_1.index(j) 14 t = al_m[b+5] 15 s_1[b1] = t 16 else: 17 pass 18 s_s = ''.join(s_1) 19 return s_s 20 21 def decoder(text): 22 al = [chr(i) for i in range(97,123)]*2 23 al_m = [chr(i) for i in range(65,91)]*2 24 text_1 = list(text) 25 for j in text_1: 26 if j in al: 27 a = al.index(j) 28 a1 = text_1.index(j) 29 t = al[a-5] 30 text_1[a1] = t 31 elif j in al_m: 32 b = al_m.index(j) 33 b1 = text_1.index(j) 34 t = al_m[b-5] 35 text_1[b1] = t 36 else: 37 pass 38 text_s = ''.join(text_1) 39 return text_s 40 41 42 43 44 text = input('请输入英文文本:') 45 46 encoded_text = encoder(text) 47 print('编码后的文本:',encoded_text) 48 49 decoded_text = decoder(encoded_text) 50 print('对编码后的文本解码:',decoded_text)
运行结果:
说明:这个有点无能为力,知道为什么第二次测试结果不对的原因,因为替换后使用index函数只会获得列表中第一个出现该字母所对应的索引,但是暂时没想到解决办法。
实验任务七
task7
源代码:
1 def collatz(n): 2 s = [] 3 for i in range(n**2): 4 s.append(n) 5 if n == 1: 6 break 7 else: 8 if n%2 == 0: 9 n = n/2 10 else: 11 n = n*3+1 12 return s 13 14 15 try: 16 n = eval(input('Enter an interger:')) 17 ans = collatz(n) 18 if n<=0: 19 raise ValueError('Error: n must be an interger') 20 except ValueError as e: 21 print(repr(e)) 22 except: 23 print('Error: n must be an interger') 24 else: 25 print(ans)
运行结果:
实验任务八
task8
源代码:
1 def func(n): 2 if n == 0: 3 return 0 4 else: 5 return func(n-1)*2+1 6 7 while True: 8 x = input() 9 if x == '#': 10 print('计算结束') 11 break 12 n = int(x) 13 ans = func(n) 14 print(f'n = {n},ans = {ans}')
运行结果:
实验结论:
对于python内置函数的使用尚且不够熟练,尚未掌握各种表示方法之间灵活变换。