Reputation: 605
I am trying to get the hang of try/ except and I just can't seem to get it working. 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
Upvotes: 0
Views: 90
Reputation: 798456
Because no exception is raised. Use an if
statement to check the trueness of the condition instead.
if h > 0:
...
elif ...:
...
else:
...
Upvotes: 3