python中assert语句

 

1、assert可以植入程序中进行程序检查

>>> a = 5
>>> b = 3
>>> assert a > 0
>>> assert a < 0
Traceback (most recent call last):
  File "<pyshell#279>", line 1, in <module>
    assert a < 0
AssertionError
>>> assert a > b
>>> assert a < b
Traceback (most recent call last):
  File "<pyshell#281>", line 1, in <module>
    assert a < b
AssertionError
>>> assert a == b
Traceback (most recent call last):
  File "<pyshell#282>", line 1, in <module>
    assert a == b
AssertionError
>>> c = "ABC"
>>> assert c == "ABC"
>>> assert c == "ABD"
Traceback (most recent call last):
  File "<pyshell#285>", line 1, in <module>
    assert c == "ABD"
AssertionError

 

posted @ 2021-02-06 23:02  小鲨鱼2018  阅读(350)  评论(0编辑  收藏  举报