实验任务1:
task1.py:
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.行1中标识符sum的作用域是当前的全局作用域,因为它没有嵌套在任何函数或类中
2.行3中标识符sum的作用域是局部作用域,因为它被赋值了int型变量
3.行6中标识符sum的作用域是当前这个函数的局部作用域,它作为一个变量被嵌套在函数中
4.行9中标识符sum的作用域是全局作用域,作为一个全局变量
实验任务2
task2_2.py:
1 list1 = [1, 9, 8, 4] 2 3 print( sorted(list1) ) 4 print( sorted(list1, reverse=True) ) 5 print( sorted(list1, True) )
运行结果截图:
问题:
python内置函数sorted()中,参数reverse的传递方式是否必须使用关键字传递?
答:是 reverse为可选参数,排序规则:reverse=True降序 reverse=False升序
task2_3.py:
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))
运行结果截图:
实验任务3
task3.py:
1 def solve(a, b, c): 2 ''' 3 求解一元二次方程, 返回方程的两个根 4 :para: a,b,c: float 方程系数 5 :return: tuple 6 ''' 7 delta = b*b - 4*a*c 8 delta_sqrt = abs(delta)**0.5 9 p1 = -b/2/a 10 p2 = delta_sqrt/2/a 11 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('输入一元二次方程系数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 # list_generator()函数定义 2 def list_generator(start, stop, step=1): 3 result = [] 4 i = start 5 while i <= stop: 6 result.append(i) 7 i += step 8 return result 9 # ××× 10 list1 = list_generator(-5, 5) 11 print(list1) 12 list2 = list_generator(-5, 5, 2) 13 print(list2) 14 list3 = list_generator(1, 5, 0.5) 15 print(list3)
运行结果截图:
实验任务5:
task5.py
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]}")
运行结果截图:
实验任务6:
task6.py
1 # 编码函数encoder()定义 2 def encoder(text): 3 result = "" 4 for c in text: 5 if 65 <= ord(c) <= 90 : 6 x = (ord(c) - ord('A') + 5) % 26 + ord('A') 7 ans = chr(x) 8 result += ans 9 elif 97 <= ord(c) <= 122 : 10 x = (ord(c) - ord('a') + 5) % 26 + ord('a') 11 ans = chr(x) 12 result += ans 13 else: 14 result += c 15 return result 16 17 # 解码函数decoder()定义 18 def decoder(text): 19 result = "" 20 for c in text: 21 if 65 <= ord(c) <= 90 : 22 x = (ord(c) - ord('A') - 5) % 26 + ord('A') 23 ans = chr(x) 24 result += ans 25 elif 97 <= ord(c) <= 122 : 26 x = (ord(c) - ord('a') - 5) % 26 + ord('a') 27 ans = chr(x) 28 result += ans 29 else: 30 result += c 31 return result 32 # 主体代码逻辑 33 text = input('输入英文文本: ') 34 encoded_text = encoder(text) 35 print('编码后的文本: ', encoded_text) 36 decoded_text = decoder(encoded_text) 37 print('对编码后的文本解码: ', decoded_text)
运行结果截图
实验任务7:
task7.py
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)
运行结果截图
实验任务8:
task8.py
1 # 函数func()定义 2 def func(n): 3 if n == 0: 4 return 0 5 return func(n-1) * 2 + 1 6 7 while True: 8 x = input() 9 if x == '#': 10 print('计算结束') 11 break 12 n = int(x) 13 ans = func(n) 14 print(f'n = {n}, ans = {ans}')
运行结果截图
标签:截图,编程,return,函数,作用域,sum,py,实验,print From: https://www.cnblogs.com/xycproduction/p/17408158.html