Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; "subject:' ": 0.07; 'string': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'wrote': 0.14; 'random': 0.14; 'be:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:[ 30': 0.16; 'subject:object': 0.16; 'variable.': 0.16; 'select': 0.22; 'issue.': 0.22; 'error': 0.23; 'first,': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'fixed': 0.29; 'leave': 0.29; 'subject:list': 0.30; 'second,': 0.31; 'subject::[': 0.31; 'lists': 0.32; 'subject:with': 0.35; 'but': 0.35; 'really': 0.36; 'next': 0.36; "i'll": 0.36; 'should': 0.36; 'list': 0.37; 'subject:]': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'dave': 0.60; 'subject:Can': 0.60; 'break': 0.61; 'you.': 0.62; 'name': 0.63; 'here': 0.66; 'skip:r 30': 0.69 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re:[Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly Date: Thu, 6 Feb 2014 01:08:38 -0500 (EST) Organization: news.gmane.org References: <6d3d99f8-637f-4249-a696-ab856b1b8682@googlegroups.com> X-Gmane-NNTP-Posting-Host: dpc6744192085.direcpc.com X-Newsreader: PiaoHong Usenet NewsReaders 1.36 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391666732 news.xs4all.nl 2957 [2001:888:2000:d::a6]:50832 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65511 dave em 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