实验任务一
实验源码
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中,共有四处有print(sum)(line1,line3,line7,line11)。这四处使用的标识符sum是同一个对象名称吗?如果不是,请以文字方式回答这4行中标识符sum的作用域。
答:不,line1内置作用域,line3为全局作用域,lline7局部作用域,line11全局作用域。
实验任务二
2-1
实验源码
1 def func1(a, b, c, d, e, f): 2 return [a,b,c,d,e,f] 3 def func2(a, b, c,*, d, e, f): 4 return [a,b,c,d,e,f] 5 def func3(a, b, c, /, d, e, f): 6 return [a,b,c,d,e,f] 7 8 print( func1(1,9,2,0,5,3) ) 9 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 10 print( func1(1,9,2, f=3, d=0, e=5)) 11 12 print( func2(11, 99, 22, d=0, e=55, f=33) ) 13 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 14 15 print( func3(111, 999, 222, 0, 555, 333)) 16 print( func3(111, 999, 222, d=0, e=555, f=333))
运行测试截图
2-2
实验源码
1 list1 = [1, 9, 8, 4] 2 print(sorted(list1)) 3 print(sorted(list1, reverse=True)) 4 print(sorted(list1, True))
运行测试截图
问题:Python内置函数sorted()中,参数revers的传递方式是否必须使用关键字传递?
答:是
2-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))
运行测试截图
实验任务三
实验源码
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 if delta >= 0: 7 root1 = p1 + p2 8 root2 = p1 - p2 9 else: 10 root1 = complex(p1, p2) 11 root2 = complex(p1, -p2) 12 return root1, root2 13 while True: 14 try: 15 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 16 if t == '#': 17 print('结束计算,退出') 18 break 19 a, b, c = map(float, t.split()) 20 if a == 0: 21 raise ValueError('a = 0, 不是一元二次方程') 22 except ValueError as e: 23 print(repr(e)) 24 print() 25 except: 26 print('有其它错误发生\n') 27 else: 28 root1, root2 = solve(a, b, c) 29 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 30 print()
运行测试截图
运行测试截图
实验任务四
实验源码
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)
运行测试截图
实验任务五
实验源码
1 def is_prime(num): 2 if num == 1: 3 return False 4 elif num == 2: 5 return True 6 else: 7 for i in range(2, num): 8 if num % i == 0: 9 return False 10 return True 11 12 def goldbach_conjecture(num): 13 primes = [] 14 for i in range(2, num): 15 if is_prime(i): 16 primes.append(i) 17 for prime in primes: 18 if num - prime in primes: 19 return f"{num} = {prime} + {num-prime}" 20 return None 21 22 for num in range(4, 21, 2): 23 expression = goldbach_conjecture(num) 24 if expression: 25 print(expression)
运行测试截图
实验任务六
实验源码
1 def encoder(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 decoder(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 25 encoded_text = encoder(text) 26 print('编码后的文本: ', encoded_text) 27 decoded_text = decoder(encoded_text) 28 print('对编码后的文本解码: ', decoded_text)
运行测试截图
实验任务七
实验源码
1 def collatz(n): 2 if n%2==0: 3 return n//2 4 else: 5 return (n*3)+1 6 7 8 try: 9 n=eval(input('Enter a positive integer:')) 10 if type(n) is float or type(n) is str or n<=0: 11 raise ValueError('Error:must be a positive integer') 12 except ValueError as e: 13 print(repr(e)) 14 print() 15 except: 16 print('Error:must be a positive integer') 17 else: 18 num=collatz(n) 19 list=[n,num] 20 while True: 21 if num!=1: 22 num=collatz(num) 23 list.append(num) 24 else: 25 break 26 print(list)
运行测试截图
实验任务八
实验源码
1 def func(n): 2 if n == 0: 3 return 1 4 else: 5 return 2 * func(n-1) + 1 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,函数,sum,编辑,num,实验,print,def From: https://www.cnblogs.com/yongzhulazhen/p/17408087.html