Python error: “IndexError: string index out of range”

前端 未结 4 663
遇见更好的自我
遇见更好的自我 2020-12-09 04:35

I\'m currently learning python from a book called \'Python for the absolute beginner (third edition)\'. There is an exercise in the book which outlines code for a hangman ga

4条回答
  •  攒了一身酷
    2020-12-09 05:05

    This error would happen when the number of guesses (so_far) is less than the length of the word. Did you miss an initialization for the variable so_far somewhere, that sets it to something like

    so_far = " " * len(word)
    

    ?

    Edit:

    try something like

    print "%d / %d" % (new, so_far)
    

    before the line that throws the error, so you can see exactly what goes wrong. The only thing I can think of is that so_far is in a different scope, and you're not actually using the instance you think.

提交回复
热议问题