【发布时间】:2021-11-29 08:50:32
【问题描述】:
我正在开发一个扣除金额功能,如果 a 它应该会引发运行时错误
这是我的代码
def deduct_amount(a, b):
try:
b - a < 0
except ValueError:
print(a + ' can not be less than' + b)
else:
c = a - b
return c
deduct_amount(8, 12)
我知道我的 try 语句有问题。如果 a 小于 b,我如何抛出值错误
【问题讨论】:
-
您不需要 try 块来引发错误。见stackoverflow.com/q/4393268/217324
标签: python try-except