实验任务1
实验源码:
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:全局变量
实验任务2.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 8 9 def func2(a, b, c,*, d, e, f): 10 ''' 11 返回参数a,b,c,d,e,f构成的列表 12 *后面的参数只能按关键字传递 13 ''' 14 return [a,b,c,d,e,f] 15 16 17 def func3(a, b, c, /, d, e, f): 18 ''' 19 返回参数a,b,c,d,e,f构成的列表 20 /前面的参数只能按位置传递 21 ''' 22 return [a,b,c,d,e,f] 23 24 25 # func1调用:按位置传递、按参数传递都可以 26 print( func1(1,9,2,0,5,3) ) 27 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 28 print( func1(1,9,2, f=3, d=0, e=5)) 29 30 31 # func2调用:d,e,f必须按关键字传递 32 print( func2(11, 99, 22, d=0, e=55, f=33) ) 33 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 34 35 36 # func3调用:a,b,c必须按位置传递 37 print( func3(111, 999, 222, 0, 555, 333)) 38 print( func3(111, 999, 222, d=0, e=555, f=333) )
运行测试截图:
变1:line33后加
print( func2(11, 99, 22, 0, 55, 33) )
运行测试截图:
变2:
line38后加
print(func3(a=111, b=999, c=222, 0, 555, 333) )
运行测试截图:
实验任务2.2
实验源码:
1 list1 = [1, 9, 8, 4] 2 print( sorted(list1) ) 3 print( sorted(list1, reverse=True) ) 4 print( sorted(list1, True) )
运行测试截图:
答:是。
实验任务2.3
实验源码:
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
实验源码:
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 if delta >= 0: 13 root1 = p1 + p2 14 root2 = p1 - p2 15 else: 16 root1 = complex(p1, p2) 17 root2 = complex(p1, -p2) 18 19 return root1, root2 20 21 22 while True: 23 try: 24 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 25 if t == '#': 26 print('结束计算,退出') 27 break 28 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()
运行测试截图:
变:
在while True前加
print(solve.__doc__) #两个下划线短线
运行测试截图:
line3-5被打印出来
实验任务4
实验源码:
1 def list_generator(a, b, c=1): 2 if type(c)== int: 3 return list(range(a, b+1, c)) 4 else: 5 lst = [a] 6 x = a 7 while x < b: 8 x += c 9 lst.append(x) 10 return lst 11 list1 = list_generator(-5, 5) 12 print(list1) 13 14 list2 = list_generator(-5, 5, 2) 15 print(list2) 16 17 list3 = list_generator(1, 5, 0.5) 18 print(list3)
运行测试截图:
实验任务5
实验源码:
1 def is_prime(n): 2 if n < 2: 3 return False 4 else: 5 for i in range(2, n): 6 if n % i == 0: 7 return False 8 else: 9 return True 10 11 prime = [] 12 for i in range(2, 21): 13 if is_prime(i): 14 prime.append(i) 15 else: 16 continue 17 18 for j in range(2, 21, 2): 19 for a in prime: 20 if (j-a) in prime and a <= (j-a): 21 print(f'{j} = {a} + {j-a}') 22 else: 23 continue
运行测试截图:
实验任务6
实验源码:
1 def encoder(x1): 2 x2 = '' 3 for i in x1: 4 if i.isalpha(): 5 if 65 <= ord(i) <= 85 or 97 <= ord(i) <= 117: 6 i = chr(ord(i)+5) 7 x2 += i 8 elif ord(i) > 85 or ord(i) > 117: 9 i = chr(ord(i)+5-26) 10 x2 += i 11 else: 12 x2 += i 13 return x2 14 15 16 def decoder(y1): 17 y2 = '' 18 for j in y1: 19 if j.isalpha(): 20 if 65 <= ord(j) <= 69 or 97 <= ord(j) <= 101: 21 j = chr(ord(j)+26-5) 22 y2 += j 23 elif ord(j) > 69 or ord(j) > 101: 24 j = chr(ord(j)-5) 25 y2 += j 26 else: 27 y2 += j 28 return y2 29 30 31 text = input('输入英文文本: ') 32 33 encoded_text = encoder(text) 34 print('编码后的文本: ', encoded_text) 35 36 decoded_text = decoder(encoded_text) 37 print('对编码后的文本解码: ', decoded_text)
运行测试截图:
实验任务7
实验源码:
1 def collatz(n): 2 lst = [n] 3 while lst[-1] != 1: 4 if lst[-1] % 2 == 0: 5 lst.append(lst[-1]//2) 6 else: 7 lst.append(lst[-1]*3+1) 8 return lst 9 10 11 try: 12 n = int(input('Enter a positive integer:')) 13 if n <= 0: 14 raise 15 except: 16 print('Error: must be a positive integer') 17 else: 18 print(collatz(n))
运行测试截图:
实验任务8
实验源码:
1 def func(n): 2 if n == 0: 3 result = 1 4 for i in range(n): 5 result *= 2 6 return result-1 7 8 9 while True: 10 x = input() 11 if x == '#': 12 print('计算结束') 13 break 14 n = int(x) 15 ans = func(n) 16 print(f'n = {n}, ans = {ans}')
运行测试截图:
标签:截图,return,函数,编程,lst,实验,print,def From: https://www.cnblogs.com/xrl1113/p/17413173.html