Game Development Reference
In-Depth Information
4 - Guess the Number
we enter the while-block again.
This is how the loop works. As long as the condition is True , the program keeps
executing the code inside the while-block repeatedly until we reach the end of the while-
block and the condition is False . And, until guessesTaken is equal to or greater than
6 , we will keep looping.
Think of the while statement as saying, "while this condition is true, keep looping
through the code in this block".
You can make this game harder or easier by changing the number of guesses the player
gets. All you have to do is change this line:
12. while guessesTaken < 6 :
into this line:
12. while guessesTaken < 4 :
...and now the player only gets four guesses instead of six guesses. By setting the
condition to guessesTaken < 4 , we ensure that the code inside the loop only runs four
times instead of six. This makes the game much more difficult. To make the game easier,
set the condition to guessesTaken < 8 or guessesTaken < 10 , which will cause
the loop to run a few more times than before and accept more guesses from the player.
Of course, if we removed line 17 altogether then the guessesTaken would never
increase and the condition would always be True . This would give the player an unlimited
number of guesses.
The Player Guesses
Lines 13 to 17 ask the player to guess what the secret number is and lets them enter their
guess. We store this guess in a variable, and then convert that string value into an integer
value.
13. print('Take a guess.') # There are four spaces in
front of print.
14. guess = input()
The program now asks us for a guess. We type in our guess and that number is stored in
a variable named guess .
Search WWH ::




Custom Search