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

D

dave em

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
 
D

dave em

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

Fixed the error and am now onto the next issue.

Solution was to return a list (I think) and then break out the components of the list and put in the variable. Here is how we did it:

secretWord = getRandomWord(words)
print('The secretWord is ' + str(secretWord[0]))
print('The secretKey is ' + str(secretWord[1]))
#Change secretWord from a list to a str
secretWord = secretWord[1]


def getRandomWord(wordDict):
# This function returns a random string from the passed dictionary of lists of strings, and the key also.
# First, randomly select a key from the dictionary:
wordKey = random.choice(list(wordDict.keys()))
print('The wordKey is ' + wordKey)
# Second, randomly select a word from the key's list in the dictionary:
wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
print('The wordIndex is ' + str(wordIndex))
print('The word is ' + wordDict[wordKey][wordIndex])
return [wordDict[wordKey][wordIndex], wordKey]

Cheers,
Dave
 
D

Dave Angel

dave em said:
Fixed the error and am now onto the next issue.

Solution was to return a list (I think) and then break out the components of the list and put in the variable. Here is how we did it:

secretWord = getRandomWord(words)
print('The secretWord is ' + str(secretWord[0]))
print('The secretKey is ' + str(secretWord[1]))
#Change secretWord from a list to a str
secretWord = secretWord[1]


def getRandomWord(wordDict):
# This function returns a random string from the passed dictionary of lists of strings, and the key also.
# First, randomly select a key from the dictionary:
wordKey = random.choice(list(wordDict.keys()))
print('The wordKey is ' + wordKey)
# Second, randomly select a word from the key's list in the dictionary:
wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
print('The wordIndex is ' + str(wordIndex))
print('The word is ' + wordDict[wordKey][wordIndex])
return [wordDict[wordKey][wordIndex], wordKey]

Much better is to change the name of the function to match what
it's really doing. But I'll leave that to you.

Make the return logic look like:

word = [wordDict[wordKey][wordIndex]
return word, wordKey

Then the calling logic should be:

secretWord, key = getRandomSomethings (words)
print('The secretWord is ' + secretWord
print('The secretKey is ' + key


This way a name is not used for contradictory purposes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top