Python assert断言+异常提示

assert的基本语法是:

"assert" expression1 ["," expression2]

expression1用于判断生成布尔值,当expression1为假的时候,则抛出异常,[]中的内容可选,即用户可以选择异常的提示值:

1
2
3
4
5
6
7
8
9
10
11
>>>a=23
>>>assert a==23
>>>a=a-1
>>>assert a==23
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError
>>>assert a==23 , "error1"
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError: error1
posted @ 2020-10-20 18:19  森西子  阅读(1734)  评论(0编辑  收藏  举报