Project: Number Guessing Game
The number guessing game is a rite of passage for new programmers. The computer holds a secret number, and each guess gets a "too high" or "too low" hint until it is correct. It brings loops and conditionals together into one satisfying build.
What You'll Build
- Guess
- Compare to secrethigh / low / correct
- Correct?stop
Step 1: Compare One Guess
For each guess, there are three cases: too low, too high, or correct. That is a clean if / elif / else:
Loading Python Playground...
Step 2: Loop Through Guesses
To simulate a full game with a predictable result, we loop over a fixed list of guesses and break when we find the answer, counting attempts as we go.
Build It Yourself
With secret = 7 and guesses = [5, 9, 7], produce the exact four lines below.
Loading Python Exercise...
Make It Real
A true game keeps guessing until it wins. This version picks a random secret and auto-plays with random guesses using while True and break. Run it a few times:
Loading Python Playground...
Challenge Ideas
- Narrow the random guesses to the range that is still possible after each hint
- Give the player only 5 attempts before the game ends
- Print all the guesses that were made, in order
Key Takeaways
- A loop plus
if / elif / elsehandles "keep trying until correct" breakends the loop the moment the guess is right- Count attempts with an accumulator (
attempts += 1) random.randint(a, b)gives a random whole number for a real game

