[英]Python Try-Except: Why does it not catch it?
I am trying to get the hang of try/ except and I just can't seem to get it working. 我试图摆脱try /的束缚,但似乎无法正常工作。 I want it to stop for a negative or a very larger number: 我希望它停止为负数或更大的数字:
try:
h > 0.
except:
return "Your distance (", h, ") is negative"
try:
h < 3700000.
except:
return "Your distance (", h, ") is outside the range this model"
else:
# Return Correct Value:
return g0*(r/(r+h))**2
Yet it keeps on going to the end no matter what value I throw at it... I've tried different things after except like false and other stuff, but none of them work: 但是不管我给它带来什么价值,它一直持续到最后……我尝试过其他不同的事情,除了错误和其他东西,但它们都不起作用:
>>> print(earth_gravity(1))
1.0
9.80649692152011
>>> print(earth_gravity(-1))
-1.0
9.806503078481338
Because no exception is raised. 因为没有引发异常。 Use an if
statement to check the trueness of the condition instead. 使用if
语句来检查条件的真实性。
if h > 0:
...
elif ...:
...
else:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.