Guess what? You’re going to create a game where the computer chooses a number and the player guesses the number.
This project teaches you how to get input from the user and how numbers work in Python (they’re different from strings). You create a random number and in the process reuse someone else’s code in your own projects. You also do a little bit of debugging and tracking down logical errors.
Plan the Game
In the guessing game, Python thinks of a random number. The player guesses the number.
- If the player guesses correctly, Python says so.
- If the player guesses incorrectly, Python says whether the number is higher or lower.
- The player keeps guessing until the player gets the right number.
If you did Project 2, you already know how to tell the player something — that’s what print is for. However, what you don’t know about the game at the moment is how to get Python to think of a random number, or how to get a guess from the player.
Get Input from the Player
In practice, your program will always need some kind of input from the user. You’re still using the command line right now, so text is the only way to get input. You get text from a user by ...