Game Development Reference
In-Depth Information
The second to last board filled with X's could not possibly have happened (unless the X
player skipped all of the O player's turns!) And the last board has strings of digits instead of
X and O, which are invalid strings for the board. But the drawBoard() function doesn't
care. It just prints the board parameter that it was passed. Computer programs only do
exactly what you tell them, even if you tell them the wrong things to do. We will just make
sure these invalid strings are not put into the passed list in the first place.
Letting the Player be X or O
21. def inputPlayerLetter():
22. # Let's the player type which letter they want to be.
23. # Returns a list with the player's letter as the
first item, and the computer's letter as the second.
24. letter = ''
25. while not (letter == 'X' or letter == 'O'):
26. print('Do you want to be X or O?')
27. letter = input().upper()
The inputPlayerLetter() is a simple function. It asks if the player wants to be X
or O, and will keep asking the player (with the while loop) until the player types in an X
or O. Notice on line 26 that we automatically change the string returned by the call to
input() to uppercase letters with the upper() string method.
The while loop's condition contains parentheses, which means the expression inside the
parentheses is evaluated first. If the letter variable was set to 'X' , the expression would
evaluate like this:
while not (letter == 'X' or letter == 'O'):
while not ('X' == 'X' or 'X' == 'O'):
while not (True or False):
while not (True):
while not True:
while False:
As you can see, if letter has the value 'X' or 'O' , then the loop's condition will be
Search WWH ::




Custom Search