建设部评职称网站,怎么查一个网站的外链,公司图案图片大全,网站备案后台#x1f349;CSDN小墨晓末:https://blog.csdn.net/jd1813346972 个人介绍: 研一#xff5c;统计学#xff5c;干货分享 擅长Python、Matlab、R等主流编程软件 累计十余项国家级比赛奖项#xff0c;参与研究经费10w、40w级横向 文… CSDN小墨晓末:https://blog.csdn.net/jd1813346972 个人介绍: 研一统计学干货分享 擅长Python、Matlab、R等主流编程软件 累计十余项国家级比赛奖项参与研究经费10w、40w级横向 文章目录 1 try/except/finally2 异常信息的显示模式2.1 异常信息显示模式1Plain2.2 异常信息显示模式1Plai22.3 异常信息显示模式3Context默认值 3 断言 【Python进阶一】——异常与错误建议收藏 该篇文章主要演示Python中的异常与错误包括try/except/finally的使用异常信息显示模式断言等内容。 1 try/except/finally 运行程序
try: #可能发生异常的语句fopen(myfile.txt,w)while True:sinput(请输入Q)if s.upper()Q:breakf.write(s\n)
except KeyboardInterrupt: #发生此异常时要执行的语句except发生其他异常时要执行的语句else无异常时要执行的语句print(程序中断)
finally:f.close()#不管是否发生异常都要执行的语句运行结果
请输入Qq2 异常信息的显示模式
2.1 异常信息显示模式1Plain 运行程序
%xmode Plain #异常信息显示模式1Plain
x1
x1运行结果
Exception reporting mode: Plain
Traceback (most recent call last):File ipython-input-37-d5101d382d83, line 3, in modulex1NameError: name x1 is not defined2.2 异常信息显示模式1Plai2 运行程序
%xmode Verbose #异常信息显示模式2Verbose
x1
x1运行结果
Traceback (most recent call last):File ipython-input-38-443ceef4ba36, line 3, in modulex1NameError: name x1 is not defined2.3 异常信息显示模式3Context默认值 运行程序
%xmode Context #异常信息显示模式3Context默认值
x1
x1运行结果
Traceback (most recent call last):File ipython-input-39-d2b0226b5ef6, line 3, in modulex1NameError: name x1 is not defined3 断言 运行程序
##断言主要用于“设置检查点”
a1
b2
assert b!0 , 分母不能等于0 #assert后未检查条件当此条件为假时抛出断言条件为真则不能抛出AssertionErrora1
b0
assert b!0 , 分母不能等于0 #条件为假抛出AssertionError运行结果
Traceback (most recent call last):File ipython-input-44-b71c74981dc7, line 3, in moduleassert b!0 , 分母不能等于0AssertionError: 分母不能等于0