实验4 函数与异常处理编程
task 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)
question:task1.py源码中,共有4处有python语句print(sum)(line1, line3, line7, line11)。这4处使用的标识符sum是代表一个变量名吗?如果不是,请分别指出这4行中变量sum的作用域。
answer:不是。
1.Built-in
2.Global
3.Local
4.Global
task 2_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 print( func1(1,9,2,0,5,3)) 26 print( func1(a = 1,b = 9,c = 2,d = 0,e = 5,f = 3)) 27 print( func1(1,9,2,f = 3,d = 0,e = 5)) 28 29 print( func2(11,99,22,d = 0,e = 55,f = 33)) 30 print( func2(a = 11,b = 99,c = 22,d = 0,e = 55,f = 33)) 31 32 print( func3(111,999,222,0,555,333)) 33 print( func3(111,999,222,d = 0,e = 555,f = 333))
line30后增加一行函数
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 print( func1(1,9,2,0,5,3)) 26 print( func1(a = 1,b = 9,c = 2,d = 0,e = 5,f = 3)) 27 print( func1(1,9,2,f = 3,d = 0,e = 5)) 28 29 print( func2(11,99,22,d = 0,e = 55,f = 33)) 30 print( func2(a = 11,b = 99,c = 22,d = 0,e = 55,f = 33)) 31 print( func2(11,99,22,0,55,33)) 32 33 print( func3(111,999,222,0,555,333)) 34 print( func3(111,999,222,d = 0,e = 555,f = 333))
错误提示信息
line33后,增加一行函数调用
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 print( func1(1,9,2,0,5,3)) 26 print( func1(a = 1,b = 9,c = 2,d = 0,e = 5,f = 3)) 27 print( func1(1,9,2,f = 3,d = 0,e = 5)) 28 29 print( func2(11,99,22,d = 0,e = 55,f = 33)) 30 print( func2(a = 11,b = 99,c = 22,d = 0,e = 55,f = 33)) 31 32 print( func3(111,999,222,0,555,333)) 33 print( func3(111,999,222,d = 0,e = 555,f = 333)) 34 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))
questionr:python内置函数sorted()中,参数reverse的传递方式是否必须使用关键字传递?
answer:是
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,e = 5,f = 6))
task3
1 def solve(a,b,c): 2 ''' 3 求解一元二次方程,返回方程的两个根 4 :param 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 12 if delta>=0: 13 root1 = p1 + p2 14 root2 = p1 - p2 15 else: 16 root1 = complex(p1,p2) 17 root2 = complex(p1,-p2) 18 19 return root1,root2 20 21 22 while True: 23 try: 24 a,b,c = eval(input('Enter eqution coefficient:')) 25 if a == 0: 26 raise 27 except: 28 print('invalid input,or,a is zero') 29 break 30 else: 31 root1,root2 = solve(a,b,c) 32 print(f'root1 = {root1:.2f},root2 = {root2:.2f}') 33 print()
在line22之前增加一行代码
1 def solve(a,b,c): 2 ''' 3 求解一元二次方程,返回方程的两个根 4 :param 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 12 if delta>=0: 13 root1 = p1 + p2 14 root2 = p1 - p2 15 else: 16 root1 = complex(p1,p2) 17 root2 = complex(p1,-p2) 18 19 return root1,root2 20 21 print(solve.__doc__) 22 while True: 23 try: 24 a,b,c = eval(input('Enter eqution coefficient:')) 25 if a == 0: 26 raise 27 except: 28 print('invalid input,or,a is zero') 29 break 30 else: 31 root1,root2 = solve(a,b,c) 32 print(f'root1 = {root1:.2f},root2 = {root2:.2f}') 33 print()
task4
1 def list_generator(a,b,c = 1): 2 y = [] 3 while a<=b: 4 y.append(a) 5 a+=c 6 return y 7 8 list1 = list_generator(-5,5) 9 print(list1) 10 list2 = list_generator(-5,5,2) 11 print(list2) 12 list3 = list_generator(1,5,0.5) 13 print(list3)
task 5
1 def is_prime(n): 2 x = 2 3 m = True 4 if n == 2: 5 return True 6 while n > x: 7 if n%x == 0 and m == True: 8 m = False 9 else: 10 x+=1 11 return m 12 13 for number in range(2,21,2): 14 for x in range(2,int(number/2)+1): 15 y = number-x 16 if is_prime(x) == True and is_prime(y) == True: 17 print('{}={}+{}'.format(number,x,y)) 18 break
task6
1 def encorder(text): 2 list = [] 3 for i in text: 4 if 65<=ord(i)<=85 or 97<=ord(i)<=117: 5 list.append(chr((ord(i)+5))) 6 elif 86<=ord(i)<=90 or 118<=ord(i)<=122: 7 list.append(chr(ord(i)-21)) 8 else: 9 list.append(i) 10 return ''.join(list) 11 12 def decorder(text): 13 list = [] 14 for i in text: 15 if 70<=ord(i)<=90 or 102<=ord(i)<=122: 16 list.append(chr((ord(i)-5))) 17 elif 65<=ord(i)<=69 or 97<=ord(i)<=101: 18 list.append(chr((ord(i)+21))) 19 else: 20 list.append(i) 21 return ''.join(list) 22 23 text=input('输入英文文本;') 24 print(f'编码后的文本;{encorder(text)}') 25 print(f'解码后的文本;{decorder(encorder(text))}')
task7
1 def collatz(n): 2 if n%2 == 0: 3 return n/2 4 else: 5 return 3*n+1 6 7 try: 8 n =int(input('Enter a positive integer:')) 9 if n > 0: 10 list = [] 11 while n != 1: 12 list.append(n) 13 n = collatz(n) 14 list.append(1) 15 print(list) 16 else: 17 print('Error:must be a positive integer') 18 except: 19 print('Error:must be a positive integer')
实验总结:
1.对于try...except...的使用仍不熟练,需要加强练习
2.对于chr(),ord()的使用仍不熟练,需要加强练习
3.对于函数的编写能力有了一定的提升,希望自己能够继续努力