Game Development Reference
In-Depth Information
loops around, guessesTaken will have the value of 1 and will be incremented to the
value 2 .
Is the Player's Guess Too Low?
Lines 19 and 20 check if the number that the player guessed is less than the secret
random number that the computer came up with. If so, then we want to tell the player that
their guess was too low by printing this message to the screen.
if Statements
19. if guess < number:
20. print('Your guess is too low.') # There are
eight spaces in front of print.
Line 19 begins an if statement with the keyword, if . Next to the if keyword is the
condition. Line 20 starts a new block (you can tell because the indentation has increased
from line 19 to line 20.) The block that follows the if keyword is called an if-block. An
if statement is used if you only want a bit of code to execute if some condition is true.
Line 19 has an if statement with the condition guess < number . If the condition
evaluates to True , then the code in the if-block is executed. If the condition is False ,
then the code in the if-block is skipped.
Like the while statement, the if
statement also has a keyword, followed
by a condition, and then a block of code.
See Figure 4-3 for a comparison of the
two statements.
The if statement works almost the
same way as a while statement, too. But
unlike the while-block, execution does
not jump back to the if statement at the
end of the if-block. It just continues on
down to the next line. In other words, if
blocks won't loop.
If the condition is True , then all the
lines inside the if-block are executed. The
only line inside this if-block on line 19 is
a print() function call.
Figure 4-3: if and while statements.
Search WWH ::




Custom Search