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


Groups > comp.lang.python > #65511

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

From Dave Angel <davea@davea.name>
Subject Re:[Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly
Date 2014-02-06 01:08 -0500
Organization news.gmane.org
References <6d3d99f8-637f-4249-a696-ab856b1b8682@googlegroups.com> <e45b079f-eb09-4aad-9393-e35f8c33d2d5@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6436.1391666732.18130.python-list@python.org> (permalink)

Show all headers | View raw


 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web