On Wednesday, February 5, 2014 7:21:29 PM UTC-7, dave em wrote:
> 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
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to