实验一
实验源码
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)task1.1py
实验截图
实验二
task2.1Python
实验源码
1 def func1(a,b,c,d,e,f): 2 return [a,b,c,d,e,f] 3 4 def func2(a,b,c,*,d,e,f): 5 return [a,b,c,d,e,f] 6 7 def func3(a,b,c,/,d,e,f): 8 return [a,b,c,d,e,f] 9 10 print(func1(1,9,2,0,5,3)) 11 print(func1(a=1,b=9,c=2,d=0,e=5,f=3)) 12 print(func1(1,9,2,d=0,e=5,f=3)) 13 14 print(func2(11,99,22,d=0,e=55,f=33)) 15 print(func2(a=11,b=99,c=22,d=0,e=55,f=33)) 16 17 18 print(func3(111,999,222,0,555,333)) 19 print(func3(111,999,222,d=0,e=555,f=333))task2.1
实验截图
task2.2Python
实验源码
1 list1 = [1,9,8,4] 2 3 print(sorted(list1)) 4 print(sorted(list1,reverse=True)) 5 print(sorted(list1,True))2.2
实验截图
task2.3Python
实验源码
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))2.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 print(solve.__doc__) 22 while True: 23 try: 24 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 25 if t == '#': 26 print('结束计算,退出') 27 break 28 a, b, c = map(float, t.split()) 29 if a == 0: 30 raise ValueError('a = 0, 不是一元二次方程') 31 except ValueError as e: 32 print(repr(e)) 33 print() 34 except: 35 print('有其它错误发生\n') 36 else: 37 root1, root2 = solve(a, b, c) 38 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 39 print()2.1
实验截图
实验四
实验源码
1 def list_generator(a,b,c=1): 2 if type(c)==type(1): 3 b = b + 1 4 lst = range(a,b,c) 5 else: 6 b = b +0.5 7 lst = [] 8 x = a 9 while (x<b): 10 lst.append(x) 11 x +=c 12 return list(lst) 13 14 15 list1 = list_generator(-5, 5) 16 print(list1) 17 18 list2 = list_generator(-5, 5, 2) 19 print(list2) 20 21 list3 = list_generator(1, 5, 0.5) 22 print(list3)View Code
实验截图
实验五
实验源码
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 = FalseView Code
实验截图
实验六
实验源码
1 def encoder(text): 2 ls=list(text) 3 for i in range(len(ls)): 4 if 'a'<=ls[i].lower()<='u': 5 ls[i]=chr(ord(ls[i])+5) 6 elif 'v'<=ls[i].lower()<='z': 7 ls[i]=chr(ord(ls[i])-21) 8 else: 9 pass 10 return ''.join(ls) 11 12 def decoder(text): 13 ls=list(text) 14 for i in range(len(ls)): 15 if 'f'<=ls[i].lower()<='z': 16 ls[i]=chr(ord(ls[i])-5) 17 elif 'a'<=ls[i].lower()<='e': 18 ls[i]=chr(ord(ls[i])+21) 19 else: 20 pass 21 return ''.join(ls) 22 23 24 text = input('输入英文文本: ') 25 26 encoded_text = encoder(text) 27 print('编码后的文本: ', encoded_text) 28 29 decoded_text = decoder(encoded_text) 30 print('对编码后的文本解码: ', decoded_text)View Code
实验七
实验源码
1 def collatz(n): 2 x=[n] 3 while x[-1]!=1: 4 if n%2==0: 5 n=n//2 6 x.append(n) 7 else: 8 n=n*3+1 9 x.append(n) 10 return x 11 12 try: 13 n=int(input('Enter a positive integer:')) 14 if n<=0: 15 raise 16 except: 17 print('Error: must be a positive integer') 18 else: 19 print(collatz(n))View Code
实验截图
实验八
实验源码
1 def func(n): 2 if n == 1: 3 ans = 2 - 1 4 else: 5 ans = 2 * func(n-1) +1 6 n -= 1 7 return ans 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}')View Code
实验截图
标签:return,源码,实验,print,True,def From: https://www.cnblogs.com/bowen666666/p/17408177.html