Game Development Reference
In-Depth Information
9 - Hangman
108. correctLetters = ''
109. secretWord = getRandomWord(words)
110. gameIsDone = False
...
144. gameIsDone = False
145. secretWord = getRandomWord(words)
146. else:
Because the
getRandomWord()
function now returns a list of two items instead of a
string,
secretWord
will be assigned a list, not a string. We would then have to change
the code as follows:
108. correctLetters = ''
109. secretWord = getRandomWord(words)
110. secretKey = secretWord[1]
111. secretWord = secretWord[0]
112. gameIsDone = False
...
144. gameIsDone = False
145. secretWord = getRandomWord(words)
146. secretKey = secretWord[1]
147. secretWord = secretWord[0]
148. else:
With the above changes,
secretWord
is first a list of two items. Then we add a new
variable named
secretKey
and set it to the second item in
secretWord
. Then we set
secretWord
itself to the first item in the
secretWord
list. That means that
secretWord
will then be a string.
Multiple Assignment
But there is an easier way by doing a little trick with assignment statements. Try typing
the following into the shell:
>>> a, b, c = ['apples', 'cats', 42]
>>> a
'apples'
>>> b
'cats'
>>> c