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)
运行截图:
答:
不是。
line1:全局作用域
line3:全局作用域
line7:局部作用域
line11:全局作用域
实验task2-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 print(func1(1,9,2,0,5,3)) 8 print(func1(a=1,b=9,c=2,d=0,e=5,f=3)) 9 print(func1(1,9,2,f=3,d=0,e=5)) 10 11 print(func2(11,99,22,d=0,e=55,f=33)) 12 print(func2(a=11,b=99,c=22,d=0,e=55,f=33)) 13 14 print(func2(111,999,222,0,555,333)) 15 print(func3(111,999,222,d=0,e=555,f=333))
运行截图:
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的传递方式必须使用关键字传递。
task2_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))
运行截图:
实验任务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 if delta >= 0: 12 root1 = p1 + p2 13 root2 = p1 - p2 14 else: 15 root1 = complex(p1, p2) 16 root2 = complex(p1, -p2) 17 return root1, root2 18 19 print(solve.__doc__) 20 while True: 21 try: 22 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 23 if t == '#': 24 print('结束计算,退出') 25 break 26 a, b, c = map(float, t.split()) 27 if a == 0: 28 raise ValueError('a = 0, 不是一元二次方程') 29 except ValueError as e: 30 print(repr(e)) 31 print() 32 except: 33 print('有其它错误发生\n') 34 else: 35 root1, root2 = solve(a, b, c) 36 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 37 print()
运行截图:
实验任务4
task4.py
实验源码:
1 def list_generator(a,b,c=1): 2 lst = [] 3 while a<=b: 4 lst.append(a) 5 a += c 6 return lst 7 8 list1 = list_generator(-5, 5) 9 print(list1) 10 list2 = list_generator(-5, 5, 2) 11 print(list2) 12 list3 = list_generator(1, 5, 0.5) 13 print(list3)
运行截图:
实验任务5
task5.py
实验源码:
1 def is_prime(n): 2 if n == 2: 3 return True 4 elif n != 1: 5 for i in range(2,n): 6 if n == 2: 7 return True 8 elif n % i == 0: 9 break 10 else: 11 return True 12 13 for i in range(2,20+1,2): 14 flag = True 15 for j in range(2, i): 16 if is_prime(j) == True and is_prime(i - j) == True and flag == True: 17 print(f'{i}={j}+{i-j}') 18 flag = False
运行截图:
实验任务6
task6.py
实验源码:
1 def encoder(text): 2 text1 = '' 3 for i in text: 4 if 'a' <= i <= 'z': 5 c = 97 + (ord(i) + 5 - 97) % 26 6 text1 += chr(c) 7 elif 'A' <= i <= 'Z': 8 c = 65 + (ord(i) + 5 - 65) % 26 9 text1 += chr(c) 10 else: 11 text1 += i 12 return text1 13 14 def decoder(text): 15 text1 = '' 16 for i in text: 17 if 'a' <= i <= 'z': 18 c = 97 + (ord(i) - 5 - 97) % 26 19 text1 += chr(c) 20 elif 'A' <= i <= 'Z': 21 c = 65 + (ord(i) - 5 - 65) % 26 22 text1 += chr(c) 23 else: 24 text1 += i 25 return text1 26 27 text = input('输入英文文本: ') 28 29 encoded_text = encoder(text) 30 print('编码后的文本: ', encoded_text) 31 32 decoded_text = decoder(encoded_text) 33 print('对编码后的文本解码: ', decoded_text)
运行截图:
实验任务7
task7.py
实验源码:
1 def collatz(n): 2 lst = [] 3 lst.append(n) 4 while n != 1: 5 if n %2 == 0: 6 n = n/2 7 lst.append(int(n)) 8 else: 9 n = n*3 +1 10 lst.append(int(n)) 11 return lst 12 try: 13 n = int(input('Enter a positive integer:')) 14 if n <= 0: 15 raise ValueError 16 except ValueError: 17 print('Error: must be a positive integer') 18 else: 19 print(collatz(n))
运行截图:
实验任务8
task8.py
实验源码:
1 def func(n): 2 if n == 0: 3 return 0 4 else: 5 return 2*func(n-1) + 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,实验,print,True,def From: https://www.cnblogs.com/zyaiyj-www-2003/p/17426064.html