1.实验任务1
task1.py
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)
2.实验任务2
task2_1.py
1 def func1(a,b,c,d,e,f): 2 ''' 3 返回参数aa,b,c,d,e,f构成的列表 4 默认,参数按位置传递;也支持关键字传递 5 ''' 6 7 return [a,b,c,d,e,f] 8 9 def func2(a,b,c,*,d,e,f): 10 ''' 11 12 返回参数a,b,c,d,e,f构成的列表 13 *后面的参数只能安关键字传递 14 ''' 15 16 return [a,b,c,d,e,f] 17 18 19 20 def func3(a,b,c,/,d,e,f): 21 ''' 22 23 返回参数a,b,c,d,e,f构成的列表 24 /前面的参数只能按位子传递 25 ''' 26 27 return [a,b,c,d,e,f] 28 29 30 #func1调用:按位置传递、按参数传递都可以 31 print( func1(1,9,2,0,5,3) ) 32 print( func1(a=1,b=9,c=2,d=0,e=5,f=3) ) 33 print( func1(1,9,2,f=3,d=0,e=5) ) 34 35 #func2调用:d,e,f必须按关键字传递 36 print( func2(11,99,22,d=0,e=55,f=33) ) 37 print( func2(a=11,b=99,c=22,d=0,e=55,f=33) ) 38 39 #func3调用:a,b,c必须按位置传递 40 print( func3(111,999,222,0,555,333) ) 41 print( func3(111,999,222,d=0,e=555,f=333) )
task2_2.py
1 list1 = [1,9,8,4] 2 3 print( sorted(list1) ) 4 print( sorted(list1,reverse=True) ) 5 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 4 print( func(1,2,3,d=4,e=5,f=6) )
3.实验任务3
task3.py
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 else: 17 root1 = complex(p1,p2) 18 root2 = complex(p1,-p2) 19 20 return root1,root2 21 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()
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 else: 17 root1 = complex(p1,p2) 18 root2 = complex(p1,-p2) 19 20 return root1,root2 21 22 print(solve.__doc__) 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()
4.实验任务4
task4.py
1 def list_generator(n,m,i=1): 2 list = [] 3 j = n 4 while j <= m: 5 list.append(j) 6 j+=i 7 return list 8 9 list1 = list_generator(-5,5) 10 print(list1) 11 12 list2 = list_generator(-5,5,2) 13 print(list2) 14 15 list3 = list_generator(1,5,0.5) 16 print(list3)
5.实验任务5
task5.py
1 def is_prime(n): 2 if n<= 1: 3 return False 4 for i in range(2,int(n**0.5)+1): 5 if n % i==0: 6 return False 7 return True 8 9 for n in range(21): 10 if n % 2 == 0: 11 for i in range(21): 12 ans1 = is_prime(i) 13 if ans1 == True: 14 j = n - i 15 ans2 = is_prime(j) 16 if ans2 == True: 17 print(f'{n} = {i} + {j}') 18 break
6.实验任务6
task6.py
1 def encoder(text): 2 text_1 = list(text) 3 text_2 = [] 4 text_3 = '' 5 for i in text_1: 6 if 'a'<=i<='z': 7 if ord(i)+5>122: 8 ans = chr(ord(i)+5-26) 9 text_2.append(ans) 10 else: 11 ans = chr(ord(i)+5) 12 text_2.append(ans) 13 elif 'A'<=i<='Z': 14 if ord(i)+5>90: 15 ans = chr(ord(i)+5-26) 16 text_2.append(ans) 17 else: 18 ans = chr(ord(i)+5) 19 text_2.append(ans) 20 else: 21 text_2.append(i) 22 for j in text_2: 23 text_3 += j 24 return(text_3) 25 26 def decoder(text): 27 text_1 = list(text) 28 text_2 = [] 29 text_3 = '' 30 for i in text_1: 31 if 'a'<=i<='z': 32 if ord(i)-5<97: 33 ans = chr(ord(i)-5+26) 34 text_2.append(ans) 35 else: 36 ans = chr(ord(i)-5) 37 text_2.append(ans) 38 elif 'A'<=i<='Z': 39 if ord(i)-5<65: 40 ans = chr(ord(i)-5+26) 41 text_2.append(ans) 42 else: 43 ans = chr(ord(i)-5) 44 text_2.append(ans) 45 else: 46 text_2.append(i) 47 for j in text_2: 48 text_3 += j 49 return(text_3) 50 51 text = input('输入英文文本:') 52 53 encoded_text = encoder(text) 54 print('编码后的文本:',encoded_text) 55 56 decoded_text = decoder(encoded_text) 57 print('对编码后的文本解码:',decoded_text)
7.实验任务7
task7.py
1 def collatz(n): 2 ans = [n] 3 while n >1: 4 if n % 2==0: 5 n = n//2 6 elif n % 2!=0: 7 n = n*3+1 8 ans.append(n) 9 return ans 10 11 try: 12 t = int(input('Enter a positive integer:')) 13 if t <= 0: 14 raise ValueError 15 except ValueError: 16 print('Error:must be positive integer') 17 else: 18 print(collatz(t))
8.实验任务8
task.py
1 def func(n): 2 if n == 0: 3 return 0 4 return 2*func(n-1)+1 5 6 while True: 7 x = input() 8 if x == '#': 9 print('计算结束') 10 break 11 n = int(x) 12 ans = func(n) 13 print(f'n = {n},ans = {ans}')
标签:return,函数,text,编程,实验,ans,print,root1,def From: https://www.cnblogs.com/wang2003chengxu/p/17413587.html