实验任务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)
回答:不是,line1是Built-in内置作用域,line3和line11是Global全局作用域,line7是Local局部作用域。
实验任务2
task2_1.py
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 # func1调用:按位置传递、按参数传递都可以 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 # func2调用:d,e,f必须按关键字传递 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 # func3调用:a,b,c必须按位置传递 15 print(func3(111, 999, 222, 0, 555, 333)) 16 print(func3(111, 999, 222, d=0, e=555, f=333))
task2_2.py
1 list1 = [1, 9, 8, 4] 2 print(sorted(list1)) 3 print(sorted(list1, reverse=True)) 4 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 print(func(1,2,3,d=4,e=5,f=6))
实验任务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()
实验任务4
task4.py
1 def list_generator(x,y,s=1): 2 l = [] 3 while x<=y: 4 l.append(x) 5 x = x+s 6 return l 7 8 list1 = list_generator(-5, 5) 9 print(list1) 10 11 list2 = list_generator(-5, 5, 2) 12 print(list2) 13 14 list3 = list_generator(1, 5, 0.5) 15 print(list3)
实验任务5
task5.py
1 def is_prime(n): 2 if n==2: 3 return True 4 else: 5 for i in range(2,int(n**0.5)+1): 6 if n%i==0: 7 return False 8 return True 9 10 for i in range(4,21,2): 11 for s in range(2,i): 12 if is_prime(i-s): 13 print(f'{i} = {s} +{i-s}') 14 break
实验任务6
task6.py
1 # 编码函数encoder()定义 2 def encoder(a): 3 c = '' 4 for i in a: 5 if i.isalpha(): 6 temp = ord(i)+5 7 if chr(temp).isalpha(): 8 c += chr(temp) 9 else: 10 c += chr(temp-26) 11 else: 12 c += i 13 return c 14 15 # 解码函数decoder()定义 16 def decoder(x): 17 z = '' 18 for i in x: 19 if i.isalpha(): 20 temp = ord(i)-5 21 if chr(temp).isalpha(): 22 z+=chr(temp) 23 else: 24 z+=chr(temp+26) 25 else: 26 z += i 27 return z 28 29 # 主体代码逻辑 30 text = input('输入英文文本: ') 31 32 encoded_text = encoder(text) 33 print('编码后的文本: ', encoded_text) 34 35 decoded_text = decoder(encoded_text) 36 print('对编码后的文本解码: ', decoded_text)
实验任务7
task7.py
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 try: 11 x = int(input('Enter a positive integer:')) 12 if x <= 0: 13 raise 14 except: 15 print('Error:must be a positive integer') 16 else: 17 print(collatz(x))
实验任务8
task8.py
1 # 函数func()定义 2 def func(n): 3 result = 1 4 for i in range(n): 5 result *= 2 6 return result-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}')
标签:11,py,return,函数,temp,编程,实验,print,def From: https://www.cnblogs.com/xq040718/p/17421937.html