Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Coding Error
#3
getRandomWord returns a list of the word and the category for the word. That list is what you are assigning to secretWord. That's why it always thinks the word is two letters long (the length of the list is two), and that's why you get the error (secretWord is a list, which doesn't convert to string implicitly for string addition).

If you change lines 129 and 164 to this:

secretWord, wordCategory = getRandomWord(words)
it fixes the problem. This is called 'unpacking', or 'tuple assignment', where you assign a sequence on the right to a set of variables on the left.

You can also use random.choice for both selections in getRandomWord:

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()))
   # Second, randomly select a word from the key's list in the dictionary:
   word = random.choice(wordDict[wordKey])
   return [word, wordKey]
You might also want to check out string formatting: https://docs.python.org/3/library/string...ing-syntax. It's more versatile than adding strings.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Python Coding Error - by MGallo - Jul-20-2017, 01:21 AM
RE: Python Coding Error - by sparkz_alot - Jul-20-2017, 12:27 PM
RE: Python Coding Error - by ichabod801 - Jul-20-2017, 12:40 PM
RE: Python Coding Error - by MGallo - Jul-20-2017, 02:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error? Mugwash 4 3,234 Apr-06-2018, 06:57 PM
Last Post: Mugwash
  New to coding. An error occurs with no definition westernwhiskey 4 3,114 Mar-15-2018, 12:37 PM
Last Post: westernwhiskey

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020