试验任务一:
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)
答:不一样,line1的sum没有定义,line3的sum作用域为全局,line7的sum的作用域为函数inc内部,line11的sum作用域为全局
试验任务二:
tack2-1:
1 def func1(a, b, c, d, e, f): 2 ''' 3 返回参数a,b,c,d,e,f构成的列表 4 默认,参数按位置传递; 也支持关键字传递 5 ''' 6 return [a,b,c,d,e,f] 7 def func2(a, b, c,*, d, e, f): 8 ''' 9 返回参数a,b,c,d,e,f构成的列表 10 *后面的参数只能按关键字传递 11 ''' 12 return [a,b,c,d,e,f] 13 def func3(a, b, c, /, d, e, f): 14 ''' 15 返回参数a,b,c,d,e,f构成的列表 16 /前面的参数只能按位置传递 17 ''' 18 return [a,b,c,d,e,f] 19 # func1调用:按位置传递、按参数传递都可以 20 print( func1(1,9,2,0,5,3) ) 21 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 22 print( func1(1,9,2, f=3, d=0, e=5)) 23 # func2调用:d,e,f必须按关键字传递 24 print( func2(11, 99, 22, d=0, e=55, f=33) ) 25 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 26 27 # func3调用:a,b,c必须按位置传递 28 print( func3(111, 999, 222, 0, 555, 333)) 29 print( func3(111, 999, 222, d=0, e=555, f=333) )
tack2-2
1 list1 = [1, 9, 8, 4] 2 print( sorted(list1) ) 3 print( sorted(list1, reverse=True) ) 4 print( sorted(list1, True) )
答:是
tack2-3
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))
试验任务三:
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 list_generator(a,b,c=1): 2 s=[] 3 while a<=b: 4 s.append(a) 5 a+=c 6 7 return s 8 9 # 待补足 10 # ××× 11 list1 = list_generator(-5, 5) 12 print(list1) 13 list2 = list_generator(-5, 5, 2) 14 print(list2) 15 list3 = list_generator(1, 5, 0.5) 16 print(list3)
试验任务五:
1 def is_prime(x): 2 j=0 3 a=0 4 for i in range(2,x//2): 5 if x%i==0: 6 a=0 7 else: 8 a+=1 9 j+=1 10 if j==a: 11 return True 12 else: 13 return False 14 n=4 15 while n<=20: 16 m=2 17 while m<n: 18 k=m 19 while k<=n: 20 if is_prime(k) and is_prime(m) and n==k+m: 21 print(f'{n}={k}+{m}') 22 k=m=100 23 k+=1 24 m+=1 25 26 n+=1
试验任务六:
1 def encoder(a): 2 c = '' 3 for i in a: 4 if i.isalpha(): 5 temp = ord(i)+5 6 if chr(temp).isalpha(): 7 c += chr(temp) 8 else: 9 c += chr(temp-26) 10 else: 11 c += i 12 return c 13 14 # 解码函数decoder()定义 15 def decoder(x): 16 z = '' 17 for i in x: 18 if i.isalpha(): 19 temp = ord(i)-5 20 if chr(temp).isalpha(): 21 z+=chr(temp) 22 else: 23 z+=chr(temp+26) 24 else: 25 z += i 26 return z 27 28 # 主体代码逻辑 29 text = input('输入英文文本: ') 30 31 encoded_text = encoder(text) 32 print('编码后的文本: ', encoded_text) 33 34 decoded_text = decoder(encoded_text) 35 print('对编码后的文本解码: ', decoded_text)
试验任务七:
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 s=0 11 while s<1: 12 13 x = int(input('Enter a positive integer:')) 14 if x > 0: 15 16 print(collatz(x)) 17 else: 18 print('Error:must be a positive integer')
试验任务八:
1 def func(n): 2 k=1 3 while n>0: 4 k=k*2 5 n-=1 6 return k-1 7 # 待补足。。。 8 while True: 9 x = input() 10 if x == '#': 11 print('计算结束') 12 break 13 n = int(x) 14 ans = func(n) 15 print(f'n = {n}, ans = {ans}')
标签:return,temp,sum,else,实验,print,def From: https://www.cnblogs.com/-bug/p/17425901.html