Jul-20-2017, 12:40 PM
(This post was last modified: Jul-20-2017, 12:41 PM by ichabod801.)
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:
You can also use random.choice for both selections in getRandomWord:
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
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures