4.函数与异常处理应用编程
1 print(sum) 2 sum = 42 3 print(sum) 4 def inc(n): 5 sum = n+1 6 print(sum) 7 return sum 8 sum = inc(7) + inc(7) 9 print(sum)
2.实验任务2
task2_1.py
def func1(a, b, c, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 默认,参数按位置传递; 也支持关键字传递 ''' return [a,b,c,d,e,f] def func2(a, b, c,*, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 *后面的参数只能按关键字传递 ''' return [a,b,c,d,e,f] def func3(a, b, c, /, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 /前面的参数只能按位置传递 ''' return [a,b,c,d,e,f] # func1调用:按位置传递、按参数传递都可以 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)) # func2调用:d,e,f必须按关键字传递 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( func2(11, 99, 22, 0, 55, 33) ) # func3调用:a,b,c必须按位置传递 print( func3(111, 999, 222, 0, 555, 333)) print( func3(111, 999, 222, d=0, e=555, f=333)) print(func3(a=111, b=999, c=222, 0, 555, 333))
task2_2.py
list1 = [1, 9, 8, 4] print( sorted(list1) ) print( sorted(list1, reverse=True) ) print( sorted(list1, True) )
task2_3.py
1 def func(a, b, c, /, *, d, e, f): 2 return( [a,b,c,d,e,f] ) 3 print(func(1,2,3,d=4,e=5,f=6))
3.实验任务3
task3.py
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
task4.py
1 def list_generator(x,y,z=1): 2 list1=[] 3 while x<=y: 4 list1.append(x) 5 x=x+z 6 return list1 7 list1=list_generator(-5,5) 8 print(list1) 9 list2=list_generator(-5,5,2) 10 print(list2) 11 list3=list_generator(1,5,0.5) 12 print(list3)
5.实验任务5
task5.py
1 def is_prime(n): 2 if n == 1: 3 return False 4 elif n == 2: 5 return True 6 else: 7 for i in range(2,n): 8 if n%i == 0: 9 return False 10 return True 11 12 13 k = 0 14 for i in range(2,21,2): 15 for j in range(1,i): 16 m = i-j 17 if is_prime(m) and is_prime(j) and j<=m: 18 print(i,'=',j,'+',m,) 19 k += 1 20 if k%6==0: 21 print('\n')
6.实验任务6
task6.py
1 x=str(input('输入英文文本:')) 2 z=0 3 for i in range(len(x)): 4 if 128>ord(x[i])+5>122 or 96>ord(x[i])+5>90: 5 a=ord(x[i])-21 6 z=z+1 7 elif 65<=ord(x[i])<=85 or 97<=ord(x[i])+5<=117: 8 a=ord(x[i])+5 9 z=z+1 10 else: 11 a=ord(x[i]) 12 z=z+1 13 if z==1: 14 print('编码后的文本',end='') 15 print(chr(a),end='') 16 print() 17 print('对编码后的文本解码',x)
7.实验任务7
task7.py
1 def collatz(n): 2 l=[n] 3 while l[-1] != 1: 4 if l[-1] % 2 == 0 : 5 l.append((l[-1])//2) 6 else: 7 l.append((l[-1])*3+1) 8 return l 9 10 try: 11 x=int(input('enter a positive integer:')) 12 if x <= 0: 13 raise 14 except: 15 print('Error: must be a positive integer') 16 else: 17 print(collatz(x))