qiuyouzhi 发表于 2020-3-21 12:55:13

Python assert 语句

Python assert 语句

语法:

assert expression [, argument]

栗子:

>>> assert True
>>> assert False # 会报错
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
    assert False # 会报错
AssertionError
>>> a = 0
>>> assert a
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
    assert a
AssertionError
>>> assert (5 == 5)
>>> a = 'q'
>>> assert (a in 'qiuyouzhi'), "错误!"
>>> a = 'f'
>>> assert (a in 'qiuyouzhi'), "错误!"
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
    assert (a in 'qiuyouzhi'), "错误!"
AssertionError: 错误!

wuqramy 发表于 2020-3-22 15:43:13

栗子:
这是你帖子中出现的奇怪的标题
页: [1]
查看完整版本: Python assert 语句