在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行时崩溃,不如在出现错误条件时就崩溃。
(资料图)
def zero(s):
a = int(s)
assert a > 0,"a超出范围" #这句的意思:如果a确实大于0,程序正常往下运行
return a
zero("-2") #但是如果a是小于0的,程序会抛出AssertionError错误,报错为参数内容“a超出范围”
报错:
Traceback (most recent call last):
File "e:\Python_list\class_student\temp.py", line 6, in <module>
zero("-2")
File "e:\Python_list\class_student\temp.py", line 3, in zero
assert a > 0,"a超出范围"
AssertionError: a超出范围