Game Development Reference
In-Depth Information
42
>>>
The trick is to put the same number of variables (delimited by commas) on the left side of
the = sign as are in the list on the right side of the = sign. Python will automatically assign
the first item's value in the list to the first variable, the second item's value to the second
variable, and so on. But if you do not have the same number of variables on the left side as
there are items in the list on the right side, the Python interpreter will give you an error.
>>> a, b, c, d = ['apples', 'cats', 42]
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
a, b, c, d = ['apples', 'cats', 42]
ValueError: need more than 3 values to unpack
>>> a, b, c, d = ['apples', 'cats']
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
a, b, c = ['apples', 'cats']
ValueError: need more than 2 values to unpack
>>>
So we should change our code in Hangman to use this trick, which will mean our
program uses fewer lines of code.
118. correctLetters = ''
119. secretWord, secretKey = getRandomWord(words)
120. gameIsDone = False
...
155. gameIsDone = False
156. secretWord, secretKey = getRandomWord(words)
157. else:
Printing the Word Category for the Player
The last change we will make is to add a simple print() call to tell the player which
set of words they are trying to guess. This way, when the player plays the game they will
know if the secret word is an animal, color, shape, or fruit. Add this line of code after line
112. Here is the original code:
 
Search WWH ::




Custom Search