1.实验任务1
task_1.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)
运行测试截图:
About sum:1.内置函数名称
2.赋值名称
3.局部变量
4.全局变量
2.实验任务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))
运行测试截图:
在最后一行加上print( func2(11, 99, 22, 0, 55, 33) )
运行测试截图:
错误原因:fun2中 d,e,f要用关键字参数输入
在最后一行加上print(func3(a=111, b=999, c=222, 0, 555, 333) )
运行测试截图:
错误原因:fun3中a,b,c只能按位置传递
task2_2.py
实验源码:
1 list1 = [1, 9, 8, 4] 2 print(sorted(list1)) 3 print(sorted(list1, reverse=True)) 4 print(sorted(list1, True))
运行测试截图:
Tip:在Python内置函数sorted()中,参数reverse的传递方式必须使用关键字传递。
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.实验任务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()
运行测试截图:
在line23前面增加一行代码:print(solve.__doc__)
实验源码:
def solve(a, b, c): ''' 求解一元二次方程, 返回方程的两个根 :para: a,b,c: float 方程系数 :return: tuple ''' delta = b*b - 4*a*c delta_sqrt = abs(delta)**0.5 p1 = -b/2/a p2 = delta_sqrt/2/a if delta >= 0: root1 = p1 + p2 root2 = p1 - p2 else: root1 = complex(p1, p2) root2 = complex(p1, -p2) return root1, root2 print(solve.__doc__) while True: try: t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') if t == '#': print('结束计算,退出') break a, b, c = map(float, t.split()) if a == 0: raise ValueError('a = 0, 不是一元二次方程') except ValueError as e: print(repr(e)) print() except: print('有其它错误发生\n') else: root1, root2 = solve(a, b, c) print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') print()
运行测试截图:
加上后solve()的说明信息line3-5被打印出来了。
4.实验任务4
task4.py
实验源码:
1 # list_generator()函数定义 2 def list_generator(x,y,s=1): 3 l = [] 4 while x<=y: 5 l.append(x) 6 x = x+s 7 return l 8 9 list1 = list_generator(-5, 5) 10 print(list1) 11 12 list2 = list_generator(-5, 5, 2) 13 print(list2) 14 15 list3 = list_generator(1, 5, 0.5) 16 print(list3)
运行测试截图:
5.实验任务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.实验任务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.实验任务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.实验任务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}')
运行测试截图:
标签:截图,return,实验,print,root1,def,root2 From: https://www.cnblogs.com/desire666/p/17408098.html