I am trying to run a small program with try/except. It should return a mark between A and F for an input value (score) between 0 and 1. When I run it works as it should, i.e.for an input of 0.6 it returns D, for an input of 0.72 it returns C and so on. If I enter a string I get the error printed after the except statement as It should be. But if I type a number higher than 1 (which is out of range (0-1)) I would like to get the same error after the except statement but I get an A. It seems that
score_float >=0.0 and score_float <= 1.0
does not work. How could I solve it? Thanks
Here is the script:
score = raw_input('Enter score: ')
try:
score_float = float(score)
score_float >=0.0 and score_float <= 1.0
if score_float < 0.6:
print 'F'
elif score_float >= 0.6 and score_float < 0.7 :
print 'D'
elif score_float >= 0.7 and score_float < 0.8 :
print 'C'
elif score_float >= 0.8 and score_float < 0.9 :
print 'B'
else:
print 'A'
except:
print 'Error, the score has to be a float number between 0.0 and 1.0'
See Question&Answers more detail:os