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)
截图:
task2-1:
1 def func1(a, b, c, d, e, f): 2 return [a,b,c,d,e,f] 3 4 5 def func2(a, b, c, *, d, e, f): 6 return [a, b, c, d, e, f] 7 8 9 def func3(a, b, c, /, d, e, f): 10 return [a, b, c, d, e, f] 11 12 print( func1(1,9,2,0,5,3) ) 13 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 14 print( func1(1,9,2, f=3, d=0, e=5)) 15 16 print( func2(11, 99, 22, d=0, e=55, f=33) ) 17 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 18 19 print( func3(111, 999, 222, 0, 555, 333)) 20 print( func3(111, 999, 222, d=0, e=555, f=333) ) 21 22 print( func3(111, 999, 222, 0, 555, 333)) 23 print( func3(111, 999, 222, d=0, e=555, f=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))
截图:
task2-3:
1 def func(a, b, c, /, *, d, e, f): 2 return([a,b,c,d,e,f]) 3 4 x= func(1, 2, 3, d=4, e=5, f=6) 5 print(x)
截图:
task3:
1 def solve(a, b, c): 2 delta = b*b - 4*a*c 3 delta_sqrt = abs(delta)**0.5 4 p1 = -b/2/a 5 p2 = delta_sqrt/2/a 6 7 if delta >= 0: 8 root1 = p1 + p2 9 root2 = p1 - p2 10 else: 11 root1 = complex(p1, p2) 12 root2 = complex(p1, -p2) 13 14 return root1, root2 15 16 17 print(solve.__doc__) 18 while True: 19 try: 20 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 21 if t == '#': 22 print('结束计算,退出') 23 break 24 a, b, c = map(float, t.split()) 25 if a == 0: 26 raise ValueError('a = 0, 不是一元二次方程') 27 except ValueError as e: 28 print(repr(e)) 29 print() 30 except: 31 print('有其它错误发生\n') 32 else: 33 root1, root2 = solve(a, b, c) 34 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 35 print()
截图:
task4:
1 def list_generator(start, stop, step=1): 2 result = [] 3 i = start 4 while i <= stop: 5 result.append(i) 6 i += step 7 return result 8 9 list1 = list_generator(-5, 5) 10 print(list1) 11 list2 = list_generator(-5, 5, 2) 12 print(list2) 13 list3 = list_generator(1, 5, 0.5) 14 print(list3)
截图:
task5:
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 def golbdach(n): 10 for i in range(2,n): 11 if is_prime(i): 12 j = n - i 13 if is_prime(j): 14 return i,j 15 return None 16 17 for i in range(4,22,2): 18 x = golbdach(i) 19 if x: 20 print(f"{i}={x[0]}+{x[1]}")
截图·:
task6:
1 def encoder(text): 2 result = "" 3 for i in text: 4 if 65 <= ord(i) <= 90: 5 x = (ord(i) - ord('A')+5)%26 +ord('A') 6 ans = chr(x) 7 result += ans 8 elif 97 <= ord(i) <= 122: 9 x = (ord(i) - ord('a')+5)%26 + ord('a') 10 ans = chr(x) 11 result += ans 12 else: 13 result += i 14 return result 15 16 def decoder(text): 17 result = "" 18 for i in text: 19 if 65 <= ord(i) <= 90: 20 x = (ord(i) - ord('A')-5)%26 +ord('A') 21 ans = chr(x) 22 result += ans 23 elif 97 <= ord(i) <= 122: 24 x = (ord(i) - ord('a')-5)%26 + ord('a') 25 ans = chr(x) 26 result += ans 27 else: 28 result += i 29 return result 30 31 text = input('输入英文文本: ') 32 encoded_text = encoder(text) 33 print('编码后的文本: ', encoded_text) 34 decoded_text = decoder(encoded_text) 35 print('对编码后的文本解码: ', decoded_text)
截图:
task7:
1 def collatz(n): 2 if n % 2 == 0: 3 return n//2 4 else: 5 return n*3 +1 6 7 result_list = [] 8 try: 9 n = int(input('Enter a positive integer:')) 10 if n <= 0: 11 raise ValueError('must be a positive integer') 12 while n != 1: 13 result_list.append(n) 14 n = collatz(n) 15 result_list.append(1) 16 except ValueError: 17 print("Error: must be a positive integer") 18 else: 19 print(result_list)
截图
task8:
1 def func(n): 2 if n == 0: 3 return 0 4 return func(n-1)*2 +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 m= {n},ans = {ans}')
截图:
标签:截图,return,sum,实验,print,root1,def From: https://www.cnblogs.com/kuma-ja/p/17408172.html