Python assert(断言)

Python assert(断言)可以分别后面的判断是否正确,如果错误会报错

作用:后面的程序需要依赖前面的条件(可以用if、else代替)

示例:

a = 1
assert type(a) is int
print('No problem')

输出结果:

No problem

a = '1'
assert type(a) is int
print('No problem')

输出结果:

Traceback (most recent call last):
   File "D:/test.py", line 3, in <module>
     assert type(a) is int
AssertionError

posted on 2019-04-07 21:47  doubtful  阅读(1901)  评论(0编辑  收藏  举报

导航