要用到的函数:
assert:
如果你断言的 语句正确 则什么反应都没有
但是如果你出错之后 就会报出 AssertionError 并且错误可以自己填写
格式 :
assert + 要判断语句 + 双引号“报错语句”
xdx = [1,2,3,4, 5]
xxx = 1
yyy = 6
assert xxx in xdx, f"{xxx} 不存在于 {xdx}"
assert yyy in xdx, "{yyy} 不存在于 {xdx}"
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
C:\Users\GEFREI~1\AppData\Local\Temp/ipykernel_20784/732228326.py in <module>
----> 1 assert yyy in xdx, "{yyy} 不存在于 {xdx}"
2 assert yyy in xdx, f"{yyy} 不存在于 {xdx}"
AssertionError: {yyy} 不存在于 {xdx}
###加了f和{}之后就会保留格式
assert yyy in xdx, f"{yyy} 不存在于 {xdx}"
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
C:\Users\GEFREI~1\AppData\Local\Temp/ipykernel_20784/78607886.py in <module>
----> 1 assert yyy in xdx, f"{yyy} 不存在于 {xdx}"
AssertionError: 6 不存在于 [1, 2, 3, 4, 5]
###这样是不对的,不能有print
assert yyy in xdx, print(f"{yyy} 不存在于 {xdx}")
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
C:\Users\GEFREI~1\AppData\Local\Temp/ipykernel_20784/210735659.py in <module>
----> 1 assert yyy in xdx, print(f"{yyy} 不存在于 {xdx}")
AssertionError: None