实验4 函数与异常处理应用编程
1.实验任务1
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)
回答问题:不是。
line1:bulit-in
line3:global
line7:local
line11:enclosing
2.实验任务2
task2_1.py
1 # task2_1.py 2 def func1(a, b, c, d, e, f): 3 return [a, b, c, d, e, f] 4 5 def func2(a, b, c, *, d, e, f): 6 return [a, b, c, d, e, f] 7 8 def func3(a, b, c, /, d, e, f): 9 return [a, b, c, d, e, f] 10 11 print(func1(1, 9, 2, 0, 5, 3)) 12 print(func1(a=1, b=9, c=2, d=0, e=5, f=3)) 13 print(func1(1, 9, 2, f=3, d=0, e=5)) 14 15 print(func2(11, 99, 22, d=0, e=55, f=33)) 16 print(func2(a=11, b=99, c=22, d=0, e=55, f=33)) 17 18 print(func3(111, 999, 222, 0, 555, 333)) 19 print(func3(111, 999, 222, d=0, e=555, f=333))
在line16后,尝试增加一行函数调用,重新运行程序
1 print( func2(11, 99, 22, 0, 55, 33) )
错误信息:
在line19后,尝试增加一行函数调用,重新运行程序
1 print(func3(a=111, b=999, c=222,0, 555, 333) )
错误信息:
task2_2.py
1 # task2_2.py 2 list1 = [1, 9, 8, 4] 3 print(sorted(list1)) 4 print(sorted(list1, reverse=True)) 5 print(sorted(list1, True))
回答问题:
参数reverse的传递方式必须使用关键字传递。
task2_3.py
1 # task2_3.py 2 def func(a, b, c, /, *, d, e, f): 3 return [a, b, c, d, e, f] 4 5 print(func(1, 2, 3, d=4, e=5, f=6))
3.实验任务3
1 def solve(a, b, c): 2 ''' 3 求解一元二次方程, 返回方程的两个根 4 :para: a,b,c: int 方程系数 5 :return: tuple 6 ''' 7 delta = b * b - 4 * a * c 8 delta_sqrt = abs(delta) ** 0.5 9 p1 = -b / 2 / a 10 p2 = delta_sqrt / 2 / a 11 if delta >= 0: 12 root1 = p1 + p2 13 root2 = p1 - p2 14 else: 15 root1 = complex(p1, p2) 16 root2 = complex(p1, -p2) 17 return root1, root2 18 print(solve.__doc__) 19 while True: 20 try: 21 a,b,c = eval(input('Enter eqution coefficient: ')) 22 if a == 0: 23 raise 24 except: 25 print('invalid input, or, a is zero') 26 break 27 else: 28 root1, root2 = solve(a, b, c) 29 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 30 print()
4.实验任务4
1 def list_generator(a, b, c=1): 2 list = [] 3 while a != b: 4 list.append(a) 5 a = a + c 6 list.append(a) 7 return list 8 9 10 list1 = list_generator(-5, 5) 11 print(list1) 12 list2 = list_generator(-5, 5, 2) 13 print(list2) 14 list3 = list_generator(1, 5, 0.5) 15 print(list3)
5.实验任务5
1 def is_prime(n): 2 a = 0 3 for i in range(2, n): 4 if n % i == 0: 5 a = a + 1 6 else: 7 a = a + 0 8 if n<=1 or a > 0: 9 return False 10 else: 11 return True 12 for i in range(0,21): 13 list=[] 14 if i%2==0: 15 for j in range(2,i): 16 if is_prime(j): 17 list.append(j) 18 else: 19 continue 20 for m in list: 21 if (i-m)in list: 22 print(f'{i}={m}+{i-m}') 23 break 24 else: 25 continue 26 else: 27 continue
6.实验任务6
1 def encoder(n): 2 import string 3 a = string.ascii_letters[5:26] + string.ascii_letters[:5]+string.ascii_letters[31:52]+string.ascii_letters[26:31] 4 trantab=n.maketrans(string.ascii_letters,a) 5 return n.translate(trantab) 6 def decoder(n): 7 import string 8 a = string.ascii_letters[5:26] + string.ascii_letters[:5] + string.ascii_letters[31:52]+string.ascii_letters[26:31] 9 trantab = n.maketrans(a,string.ascii_letters) 10 return n.translate(trantab) 11 n=input('输入英文文本:') 12 print(f'编码后的文本:{encoder(n)}') 13 print(f'对编码后的文本解码:{decoder(encoder(n))}')
7.实验任务7
1 def collatz(n): 2 if n%2==0: 3 n=n/2 4 else: 5 n=3*n+1 6 return int(n) 7 try: 8 n=eval(input('Enter a positive integer:')) 9 if n<=0 or type(n)!=int: 10 raise 11 except: 12 print('Error: must be a positive integer') 13 else: 14 list=[] 15 while n!=1: 16 list.append(n) 17 n=collatz(n) 18 list.append(n) 19 print(list)
实验总结:
python真的好难,好累哟。:)