The computer chooses a number. Then it asks you to guess it.
When you guess, it tells you if your number is too low, too high, or if you got it!
Then it lets you guess again, and again, until you get it. To make the program, use a “while True:” loop. To get out of the loop, you use the command “break” when its time to leave.
Improving the number game
There is a lot more you can do to make the number game better. Some are:
- Have it count your guesses, and either limit you to some number (5?) or tell you how many guesses it took you.
- Once you’ve won, let you play again (with the computer choosing a different number)
- Have the computer tell you “you’re really close!” when you’re really close.
I’ve done this with students for many years, and have seen some really great quirky programs. What if you guess 23 and the computer tells you
“You’re low, and off by an even multiple of 7!”
“You’re high, and off by a prime number!”
These kinds of things are pretty hard to do, but can make the game fun. Don’t be afraid to ask how to do a thing!