Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65500 > unrolled thread

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

Started bydave em <daveandem2000@gmail.com>
First post2014-02-05 18:21 -0800
Last post2014-02-06 17:23 +1100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Help with TypeError: Can't convert 'list' object to str implicitly dave em <daveandem2000@gmail.com> - 2014-02-05 18:21 -0800
    [Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly dave em <daveandem2000@gmail.com> - 2014-02-05 18:49 -0800
      Re:[Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly Dave Angel <davea@davea.name> - 2014-02-06 01:08 -0500
      Re: [Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly Chris Angelico <rosuav@gmail.com> - 2014-02-06 17:23 +1100

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

Fromdave em <daveandem2000@gmail.com>
Date2014-02-05 18:21 -0800
SubjectHelp with TypeError: Can't convert 'list' object to str implicitly
Message-ID<6d3d99f8-637f-4249-a696-ab856b1b8682@googlegroups.com>
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

[toc] | [next] | [standalone]


#65501 — [Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly

Fromdave em <daveandem2000@gmail.com>
Date2014-02-05 18:49 -0800
Subject[Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly
Message-ID<e45b079f-eb09-4aad-9393-e35f8c33d2d5@googlegroups.com>
In reply to#65500
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

[toc] | [prev] | [next] | [standalone]


#65511 — Re:[Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly

FromDave Angel <davea@davea.name>
Date2014-02-06 01:08 -0500
SubjectRe:[Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly
Message-ID<mailman.6436.1391666732.18130.python-list@python.org>
In reply to#65501
 dave em <daveandem2000@gmail.com> Wrote in message:
>
> 
> 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.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#65512 — Re: [Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly

FromChris Angelico <rosuav@gmail.com>
Date2014-02-06 17:23 +1100
SubjectRe: [Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly
Message-ID<mailman.6437.1391667816.18130.python-list@python.org>
In reply to#65501
On Thu, Feb 6, 2014 at 5:08 PM, Dave Angel <davea@davea.name> wrote:
> print('The secretWord is ' + secretWord
> print('The secretKey is ' + key

))

http://xkcd.com/859/

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web