任务一
实验代码
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 11 sum = inc(7) + inc(7) 12 print(sum)task
运行结果截图
不是同一个对象名称
1.Built-in。2和3 Global。4.Local
任务二
实验代码
1 def func1(a, b, c, d, e, f): 2 return [a,b,c,d,e,f] 3 4 5 def func2(a, b, c,*, d, e, f): 6 return [a,b,c,d,e,f] 7 8 9 def func3(a, b, c, /, d, e, f): 10 return [a,b,c,d,e,f] 11 12 13 # func1调用:按位置传递、按参数传递都可以 14 print( func1(1,9,2,0,5,3) ) 15 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 16 print( func1(1,9,2, f=3, d=0, e=5)) 17 18 19 # func2调用:d,e,f必须按关键字传递 20 print( func2(11, 99, 22, d=0, e=55, f=33) ) 21 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 22 23 24 # func3调用:a,b,c必须按位置传递 25 print( func3(111, 999, 222, 0, 555, 333)) 26 print( func3(111, 999, 222, d=0, e=555, f=333) )task1
运行结果截图
实验代码
1 1 list1 = [1, 9, 8, 4] 2 2 print( sorted(list1) ) 3 3 print( sorted(list1, reverse=True) ) 4 4 print( sorted(list1, True) )task2
运行结果截图
是
实验代码
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))task3
运行结果截图
实验代码
任务三
实验代码
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 while True: 19 try: 20 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 21 if t == '#': 22 print('结束计算,退出') 23 break 24 a, b, c = map(float, t.split()) 25 if a == 0: 26 raise ValueError('a = 0, 不是一元二次方程') 27 except ValueError as e: 28 print(repr(e)) 29 print() 30 except: 31 print('有其它错误发生\n') 32 else: 33 root1, root2 = solve(a, b, c) 34 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 35 print()task
运行结果截图
任务四
实验代码
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)task
运行结果截图
任务五
实验代码
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 breaktask5
运行结果截图
任务六
实验代码
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)task6
运行结果截图
任务七
实验代码
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))task
运行结果截图
任务八
实验代码
1 def func(a): 2 if a == 0: 3 return 0 4 else: 5 return 2*func(a-1)+1 6 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}')task
运行结果截图
标签:11,截图,return,else,实验,print,def From: https://www.cnblogs.com/sqdl666/p/17425363.html