Help with TypeError: Can't convert 'list' object to str implicitly

dave em daveandem2000 at gmail.com
Wed Feb 5 21:21:29 EST 2014


Hello,

Background.  My 11 y/o son and I have taken on the task to learn python and work our way through the http://inventwithpython.com/chapters/ book.
-  We are currently on Chapter 9 and trying to modify the hangman program.

- the first challenge was to modify the word list into a dictionary.  So we changed our words variable into a dictionary
--  I believe we did this correctly.

-  But we somehow broke the game.  All of the words are only two letters and the program crashes on exit with the following error.

 Traceback (most recent call last):
  File "/media/.../Python/hangman.py", line 155, in <module>
    print('You have run out of guesses! \n After ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
TypeError: Can't convert 'list' object to str implicitly

-I can't see why this wouldn't work.  By definition isn't this the cast:

1)  len(correctLetters) //returns the lengths of the variable as an int
2)  str(len(correctLetters)) // converts the integer into a string.

Applicable code is here:
 # Check if player has guessed too many times and lost
        if len(missedLetters) == len(HANGMANPICS) - 1:
            displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
            print('You have run out of guesses! \n After ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
            gameIsDone = True

Any help to get us past this error message is most appreciated.

Thanks in advance,
Dave



More information about the Python-list mailing list