Game Development Reference
In-Depth Information
4 - Guess the Number
Because this program is a game, we'll call the user the player , but the word "user"
would be correct too.
Sample Run of "Guess the Number"
Here is what our game will look like to the player when the program is run. The text that
the player types in is in bold .
Hello! What is your name?
Albert
Well, Albert, I am thinking of a number between 1
and 20.
Take a guess.
10
Your guess is too high.
Take a guess.
2
Your guess is too low.
Take a guess.
4
Good job, Albert! You guessed my number in 3
guesses!
Enter this code exactly as it appears here, and then save it by clicking on the File menu
and then Save As . Give it a file name like guess.py then run it by pressing the F5 key. Don't
worry if you don't understand the code now, I'll explain it step by step.
Guess the Number's Source Code
Here is the source code for our Guess the Number game. When you enter this code into
the file editor, be sure to pay attention to the spacing at the front of some of the lines. Some
lines have four or eight spaces in front of them. After you have typed in the code, save the
file as guess.py . You can run the program from the file editor by pressing F5. If you see an
error message, check that you have typed the program in exactly as written.
If you don't want to type all this code, you can download it from this topic's website at
the URL http://inventwithpython.com/chapter4.
Important Note! Be sure to run this program with Python 3, and not Python 2. The
programs in this topic use Python 3, and you'll get errors if you try to run them with Python
2. You can click on Help and then About IDLE to find out what version of Python you
have.
Search WWH ::




Custom Search