实验任务一
实验源码:
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)
实验截图:
实验任务二
实验源码:
1 def func1(a, b, c, d, e, f): 2 return[a, b, c, d, e, f] 3 4 def func2(a, b, c,*, d, e, f): 5 return[a, b, c, d, e, f] 6 7 def func3(a, b, c, /, d, e, f): 8 return[a,b,c,d,e,f] 9 10 print(func1(1,9,2,0,5,3)) 11 print(func1(a=1, b=9, c=2, d=0, e=5, f=3)) 12 print(func1(1,9,2, f=3, d=0, e=5)) 13 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 19 print(func3(111,999,222,0,555,333)) 20 print(func3(111,999,222,d=0,e=55,f=333))
实验截图:
实验源码:
1 list1 = [1, 9, 8, 4] 2 3 print( sorted(list1) ) 4 print( sorted(list1, reverse=True) ) 5 print( sorted(list1, True) )
实验截图:
实验源码:
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))
实验截图:
实验结论:python内置函数sorted()中,参数reverse的传递方式必须使用关键字传递
实验任务三
实验源码:
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() 26 if t == '#': 27 print('结束计算,退出') 28 break 29 a, b, c = map(float, t.split()) 30 31 if a == 0: 32 raise ValueError('a = 0, 不是一元二次方程') 33 34 except ValueError as e: 35 print(repr(e)) 36 print() 37 except: 38 print('有其他错误发生\n') 39 else: 40 root1, root2 = solve(a, b, c) 41 print(f'roo1 = {root1:.2f}, root2 = {root2:.2f}') 42 print()
实验截图:
实验任务四
实验源码:
1 def list_generator(a,b,c=1): 2 lst = [] 3 while a<=b: 4 lst.append(a) 5 a += c 6 return lst 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)
实验截图:
实验任务五
实验源码:
1 def is_prime(x): 2 if x < 2: 3 return False 4 5 else: 6 for i in range(2, x): 7 if x % i == 0: 8 return False 9 break 10 else: 11 return True 12 13 x = [] 14 for i in range(2, 21): 15 if is_prime(i): 16 x.append(i) 17 else: 18 continue 19 20 for j in range(2, 21, 2): 21 for a in x: 22 if (j-a) in x and a <= (j-a): 23 print(f'{j} = {a} + {j-a}') 24 else: 25 continue
实验截图:
实验任务六
实验源码:
1 def encoder(n): 2 ls = list(n) 3 for i in range(len(ls)): 4 if 'a' <= ls[i].lower() <= 'u': 5 ls[i] = chr(ord(ls[i]) + 5) 6 elif 'v'<=ls[i].lower()<='z': 7 ls[i] = chr(ord(ls[i]) - 21) 8 else: 9 pass 10 return ''.join(ls) 11 12 def decoder(n): 13 ls = list(n) 14 for i in range(len(ls)): 15 if 'f' <= ls[i].lower() <= 'z': 16 ls[i] = chr(ord(ls[i]) - 5) 17 elif 'a'<=ls[i].lower()<='e': 18 ls[i] = chr(ord(ls[i]) + 21) 19 else: 20 pass 21 return ''.join(ls) 22 23 24 text = input('输入英文文本: ') 25 encoded_text = encoder(text) 26 print('编码后的文本: ', encoded_text) 27 decoded_text = decoder(encoded_text) 28 print('对编码后的文本解码: ', decoded_text)
实验截图:
实验任务七:
实验源码:
1 def collatz(n): 2 lst = [n] 3 while lst[-1] != 1: 4 if lst[-1] % 2 == 0: 5 lst.append(lst[-1]//2) 6 else: 7 lst.append(lst[-1]*3+1) 8 return lst 9 10 11 try: 12 n = int(input('Enter a positive integer:')) 13 if n <= 0: 14 raise 15 except: 16 print('Error: must be a positive integer') 17 else: 18 print(collatz(n))
实验截图:
实验任务八:
实验源码:
1 def func1(n): 2 if n == 1: 3 return 2 4 else: 5 return 2*func1(n - 1) 6 def func(n): 7 return func1(n) - 1 8 9 while True: 10 x = input() 11 if x == '#': 12 print('计算结束') 13 break 14 n = int(x) 15 ans = func(n) 16 print(f'n = {n}, ans = {ans}')
实验截图:
标签:截图,return,源码,实验,print,def From: https://www.cnblogs.com/qjx666/p/17408146.html