task.1
print(sum) sum = 42 print(sum) def inc(n): sum = n+1 print(sum) return sum sum = inc(7) + inc(7) print(sum)
About sum:1.内置函数名称
2.赋值名称
3.局部变量
4.全局变量
task.2
def func1(a, b, c, d, e, f): return [a,b,c,d,e,f] def func2(a, b, c,*, d, e, f): return [a,b,c,d,e,f] def func3(a, b, c, /, d, e, f): return [a,b,c,d,e,f] # func1调用:按位置传递、按参数传递都可以 print(func1(1,9,2,0,5,3)) print(func1(a=1, b=9, c=2, d=0, e=5, f=3)) print(func1(1,9,2, f=3, d=0, e=5)) # func2调用:d,e,f必须按关键字传递 print(func2(11, 99, 22, d=0, e=55, f=33)) print(func2(a=11, b=99, c=22, d=0, e=55, f=33)) # func3调用:a,b,c必须按位置传递 print(func3(111, 999, 222, 0, 555, 333)) 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只能按位置传递
实验原码
1 list1 = [1, 9, 8, 4] 2 print(sorted(list1)) 3 print(sorted(list1, reverse=True)) 4 print(sorted(list1, True))
运行测试图
实验源码
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))
运行测试截图:
task.3
实验源码:
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 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()
运行测试截图:
实验源码
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()
运行测试截图
task.4
实验源码:
# list_generator()函数定义 def list_generator(x,y,s=1): l = [] while x<=y: l.append(x) x = x+s return l list1 = list_generator(-5, 5) print(list1) list2 = list_generator(-5, 5, 2) print(list2) list3 = list_generator(1, 5, 0.5) print(list3)
运行测试截图:
task.5
实验源码:
def is_prime(n): if n==2: return True else: for i in range(2,int(n**0.5)+1): if n%i==0: return False return True for i in range(4,21,2): for s in range(2,i): if is_prime(i-s): print(f'{i} = {s} +{i-s}') break
运行测试截图:
task.6
实验源码:
# 编码函数encoder()定义 def encoder(a): c = '' for i in a: if i.isalpha(): temp = ord(i)+5 if chr(temp).isalpha(): c += chr(temp) else: c += chr(temp-26) else: c += i return c # 解码函数decoder()定义 def decoder(x): z = '' for i in x: if i.isalpha(): temp = ord(i)-5 if chr(temp).isalpha(): z+=chr(temp) else: z+=chr(temp+26) else: z += i return z # 主体代码逻辑 text = input('输入英文文本: ') encoded_text = encoder(text) print('编码后的文本: ', encoded_text) decoded_text = decoder(encoded_text) print('对编码后的文本解码: ', decoded_text)
task.7
def collatz(n): l = [n] while l[-1] !=1: if l[-1]%2==0: l.append(l[-1]//2) else: l.append(l[-1]*3+1) return l try: x = int(input('Enter a positive integer:')) if x <= 0: raise except: print('Error:must be a positive integer') else: print(collatz(x))
task.8
# 函数func()定义 def func(n): result = 1 for i in range(n): result *= 2 return result-1 while True: x = input() if x == '#': print('计算结束') break n = int(x) ans = func(n) print(f'n = {n}, ans = {ans}')
标签:p2,return,函数,编程,实验,print,root1,def,root2 From: https://www.cnblogs.com/zdsny/p/17415126.html