断言语句和if语句分子有点像,它用于对一个bool表达式进行断言,如果该bool表达式为True,该程序可以向下执行:否则程序会应答AssertionError
age = int(input()) 2 assert 20 < age < 80 3 print("范围在20-80之间")
运行结果
45
范围在20-80之间
ztr@ztr-G3-3579:~/myself/python/practice$ python3 test_assert.py
12
Traceback (most recent call last):
File "test_assert.py", line 2, in <module>
assert 20 < age < 80
AssertionError