实验4 函数与异常处理应用编程
2022/5/11 14:32:44
本文主要是介绍实验4 函数与异常处理应用编程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
task1
print(sum) sum = 42 print(sum) def inc(n): sum = n+1 print(sum) return sum sum = inc(7) + inc(7) print(sum)
不是。
line1,全局 line3,全局 line7,局部 line11,全局task2_1
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] 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)) 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) ) print( func3(111, 999, 222, 0, 555, 333)) print( func3(111, 999, 222, d=0, e=555, f=333) )
task2_2
list1 = [1, 9, 8, 4] print( sorted(list1) ) print( sorted(list1, reverse=True) ) print( sorted(list1, True) )
python内置函数sorted()中,参数reverse的传递方式必须使用关键字传递
task2_3
def func(a, b, c, /, *, d, e, f): return( [a,b,c,d,e,f] ) print(func(1,2,3,d=4,e=5,f=6))
task3
def solve(a, b, c): ''' 求解一元二次方程, 返回方程的两个根 :para: a,b,c: int 方程系数 :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: a,b,c = eval(input('Enter eqution coefficient: ')) if a == 0: raise except: print('invalid input, or, a is zero') break else: root1, root2 = solve(a, b, c) print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') print()
重新运行,函数solve()的说明信息(line3-5)被打印出来了
task4
def list_generator(a,b,c=1): list=[] while a<=b: list.append(a) a+=c return list list1 = list_generator(-5, 5) print(list1) list2 = list_generator(-5, 5, 2) print(list2) list3 = list_generator(1, 5, 0.5) print(list3)
task5
def is_prime(a): sum=0 if a<2: return False elif a==2: return True for i in range(2,a): if a%i==0: return False return True for i in range(2,11): n = 2*i for j in range(2,n): if is_prime(j) == True: if is_prime(n - j) == True: print("{}={}+{}".format(n, j, n - j)) break
task6
def encoder(): print("编码后的文本:",end = '') for p in plaincode: if ord("a") <= ord(p) <= ord("z"): print(chr(ord("a") + (ord(p) - ord("a") + 5) % 26), end='') elif ord("A") <= ord(p) <= ord("Z"): print(chr(ord("A") + (ord(p) - ord("A") + 5) % 26), end='') else: print(p,end='') return ' ' def decoder(): print("对编码后的文本解码:",end ='') print(plaincode) shadowcode = '' for q in shadowcode: if ord("a") <= ord(q) <= ord("z"): print((chr(ord("a") + (ord(q) - ord("a") - 5) % 26)), end='') if ord("A") <= ord(q) <= ord("Z"): print((chr(ord("A") + (ord(q) - ord("A") - 5) % 26)), end='') else: print(q, end='') print() return '其实上面是错的,实在写不出来拿了个答案一样的凑一下数' plaincode = input("请输入英文文本:") print(encoder()) print(decoder())
task7
def collatz(x): if (x % 2 == 0): x = int(x / 2) else: x = 3 * x + 1 return x list = [] for i in range(1, 6): try: n = eval(input('Enter a positive integer: ')) if (n <= 0) or (int(n) != n): raise while True: if (n == 1): list.append(n) break else: list.append(int(n)) n = collatz(n) print(list) except: print('Error: must be a positive integer')
这篇关于实验4 函数与异常处理应用编程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)