I've been working my way through the Python for Everybody book by Charles Severance. I'm stuck on one of the problems, even a worked example online doesn't seem to fully answer the question.
Exercise 7: Rewrite the grade program from the previous chapter using a function called computegrade that takes a score as its parameter and returns a grade as a string.
Score >= 0.9 A >= 0.8 B >= 0.7 C >= 0.6 D <0.6 F Enter score: 0.95 A Enter score: perfect Bad score Enter score: 10.0 Bad score Enter score: 0.75 C Enter score: 0.5 F
Run the program repeatedly to test the various different values for input.
My code is:
def computegrade(score):
if float(score)>1:
return 'Bad score'
elif float(score)>=0.9:
return 'A'
elif float(score)>=0.8:
return 'B'
elif float(score)>=0.7:
return 'C'
elif float(score)>=0.6:
return 'D'
elif float(score)<0.6:
return 'F'
else:
return 'Bad score'
...which seems to work until I type in "perfect" or any kind of non-numerical input (throws an error). Just wondering why this doesn't work with my else
statement, when word inputs don't fit other criteria?