[Tutor] bug in exam score conversion program

David ldl08 at gmx.net
Sat Oct 4 12:11:35 CEST 2008


I just completed exercise 7 (chapter 4) in Zelle's book:
"A certain CS professor gives 100-point exams that are graded on the 
scale 90–100:A, 80–89:B, 70–79:C, 60–69:D, 60:F. Write a program that 
accepts an exam score as input and prints out the corresponding grade."

I am quite happy with my code, but there is a bug: if the score is 100, 
then the program calculates 100/10 = 10. However, the tuple runs to 9, 
leaving me with an error message: IndexError: tuple index out of range

I can't figure out how to solve that problem...
I also suspect that my code clearly exposes me as a beginner :-) What 
would be the pythonic way of solving that exercise?

# exam score to grade conversion
# Zelle, ch. 4, exercise 7
x = ("F", "F", "F", "F", "F", "E", "D", "C", "B", "A")
score = raw_input("What's your exam score (0-100)? ")
grade = x[int(score)/10]
print "Your grade is:", grade

I greatly appreciate your comments!


More information about the Tutor mailing list