[Tutor] bug in exam score conversion program

Sander Sweers sander.sweers at gmail.com
Sat Oct 4 13:17:05 CEST 2008


On Sat, Oct 4, 2008 at 12:11, David <ldl08 at gmx.net> wrote:
> 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

Python starts counting from zero so to access the first item in x it
would be x[0]. When you do x[10] you actually are requestting the 11th
value in x instead of the 10th and this does not exist.

You should be able to fix this yourself now.

Greets
Sander


More information about the Tutor mailing list