Game Development Reference
In-Depth Information
If we replace the list slices and the list indexes with the values that they represent, the
unrolled loop code would be the same as this:
if 'o' in 'tr': # Condition is False, blanks ==
'_____'
blanks = '' + 'o' + '____' # This line is skipped.
if 't' in 'tr': # Condition is True, blanks ==
'_____'
blanks = '_' + 't' + '___' # This line is
executed.
if 't' in 'tr': # Condition is True, blanks ==
'_t___'
blanks = '_t' + 't' + '__' # This line is
executed.
if 'e' in 'tr': # Condition is False, blanks ==
'_tt__'
blanks = '_tt' + 'e' + '_' # This line is skipped.
if 'r' in 'tr': # Condition is True, blanks ==
'_tt__'
blanks = '_tt_' + 'r' + '' # This line is
executed.
# blanks now has the value '_tt_r'
The above three code examples all do the
same thing
(at least, they do when
secretWord
is
'otter'
and
correctLetters
is
'tr'
. The first box is the actual
code we have in our game. The second box shows code that does the same thing except
without a
for
loop. The third box is the same as the second box, except we have evaluated
many of the expressions in the second box.
The next few lines of code display the new value of
blanks
with spaces in between
each letter.
81. for letter in blanks: # show the secret word with
spaces in between each letter
82. print(letter, end=' ')
83. print()
This
for
loop will print out each character in the string
blanks
. Remember that by
now,
blanks
may have some of its underscores replaced with the letters in
secretWord
. The
end
keyword argument in line 82's
print()
call makes the
print
()
function put a space character at the end of the string instead of a newline character.
This is the end of the
displayBoard()
function.