Game Development Reference
In-Depth Information
Figure 9-3 is an example of
elif
statements. Unless these three conditions are all
False
, the player will keep looping and keep being asked for a letter. But when all three
of the conditions are
False
, then the else-block's
return
statement will run and we will
exit this loop and function.
Figure 9-3: The
elif
statement.
Asking the Player to Play Again
100. def playAgain():
101. # This function returns True if the player wants to
play again, otherwise it returns False.
102. print('Do you want to play again? (yes or no)')
103. return input().lower().startswith('y')
The
playAgain()
function has just a
print()
function call and a
return
statement. The
return
statement has an expression that looks complicated, but we can
break it down. Once we evaluate this expression to a value, that value will be returned from
this function.
The expression on line 103 doesn't have any operators, but it does have a function call
and two method calls. The function call is
input()
and the method calls are
lower()
and
startswith('y')
. Remember that method calls are function calls that are attached
by a period to the
value
on their
left
.
lower()
is attached to the return value of
input
()
.
input()
returns a string of the text that the user typed in. Here's a step by step look at
how Python evaluates this expression if the user types in YES.
return input().lower().startswith('y')
return 'YES'.lower().startswith('y')