实验4 函数与异常处理应用编程
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)
截图:

line1:内置函数名
line3:赋值名
line7:局部变量
line11:全局变量
task2_1
1 def func1(a, b, c, d, e, f): 2 ''' 3 返回参数a,b,c,d,e,f构成的列表 4 默认,参数按位置传递; 也支持关键字传递 5 ''' 6 return [a,b,c,d,e,f] 7 8 9 def func2(a, b, c,*, d, e, f): 10 ''' 11 返回参数a,b,c,d,e,f构成的列表 12 *后面的参数只能按关键字传递 13 ''' 14 return [a,b,c,d,e,f] 15 16 17 def func3(a, b, c, /, d, e, f): 18 ''' 19 返回参数a,b,c,d,e,f构成的列表 20 /前面的参数只能按位置传递 21 ''' 22 return [a,b,c,d,e,f] 23 24 25 # func1调用:按位置传递、按参数传递都可以 26 print( func1(1,9,2,0,5,3) ) 27 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 28 print( func1(1,9,2, f=3, d=0, e=5)) 29 30 31 # func2调用:d,e,f必须按关键字传递 32 print( func2(11, 99, 22, d=0, e=55, f=33) ) 33 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 34 35 36 # func3调用:a,b,c必须按位置传递 37 print( func3(111, 999, 222, 0, 555, 333)) 38 print( func3(111, 999, 222, d=0, e=555, f=333) )
截图:

在line33后,尝试增加一行函数调用,重新运行程序,查看解释器中出现的错误提示信息是什么。
1 print( func2(11, 99, 22, 0, 55, 33) )

func2函数要键入3个参数,而不是6个。d,e,f 需要用关键字参数传入。
在line38后,尝试增加一行函数调用,重新运行程序,查看解释器中出现的错误提示信息是什么。
1 print(func3(a=111, b=999, c=222, 0, 555, 333) )

前面的参数只能按位置传递
task2_2
1 list1 = [1, 9, 8, 4] 2 print( sorted(list1) ) 3 print( sorted(list1, reverse=True) ) 4 print( sorted(list1, True) )
截图:

reversed必须要关键字参数传入
task2_3
1 def func(a, b, c, /, *, d, e, f): 2 return([a, b, c, d, e, f]) 3 4 5 print(func(1, 2, 3, d=4, e=5, f=6))
截图:

task3
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 19 20 while True: 21 try: 22 a,b,c = eval(input('Enter eqution coefficient: ')) 23 if a == 0: 24 raise 25 except: 26 print('invalid input, or, a is zero') 27 break 28 else: 29 root1, root2 = solve(a, b, c) 30 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 31 print()
截图:

试在line22前面增加一行代码:
1 print(solve.__doc__)

函数solve()的说明信息(line3-5)被打印出来了
task4
1 def list_generator(a,b,step=1): 2 output = [] 3 while a <= b : 4 output.append(a) 5 a += step 6 return output 7 8 list1 = list_generator(-5, 5) 9 print(list1) 10 11 list2 = list_generator(-5, 5, 2) 12 print(list2) 13 14 list3 = list_generator(1, 5, 0.5) 15 print(list3)
截图:

task5
1 def is_Preme(n): 2 for i in range(2,int(n**0.5)+1): 3 if not n%i : 4 return False 5 else : 6 return True 7 8 for i in range(2,21,2): 9 for sim in range(2,i): 10 if is_Preme(i-sim): 11 print(f"{i} = {sim} + {i-sim}") 12 break
截图:

task6
1 def encode(text): 2 sentence = [] 3 for i in text: 4 if i.isupper(): 5 sentence.append(chr((ord(i)-60)%26+65)) 6 elif i.islower(): 7 sentence.append(chr((ord(i)-92)%26+97)) 8 else : 9 sentence.append(i) 10 return ''.join(sentence) 11 12 13 def decode(text): 14 sentence = [] 15 for i in text: 16 if i.isupper(): 17 sentence.append(chr((ord(i)-44)%26+65)) 18 elif i.islower(): 19 sentence.append(chr((ord(i)-76)%26+97)) 20 else : 21 sentence.append(i) 22 return ''.join(sentence) 23 24 text = input("输入:") 25 print(f"编码后:{encode(text)}") 26 print(f"解码:{decode(encode(text))}")
截图:

task7
1 def collatz(n): 2 num = [n] 3 while num[-1] != 1: 4 if not num[-1] % 2: 5 num.append(int(num[-1]/2)) 6 else: 7 num.append(int(3*num[-1]+1)) 8 return num 9 10 11 try: 12 n = eval(input("输入正整数:")) 13 if n <= 0 or int(n) != n: 14 raise 15 except: 16 print("输入错误, 不是一个有效的整数...") 17 else: 18 print(collatz(n))
截图:

总结:
python真的好难,yue

浙公网安备 33010602011771号