Stop 03 · Control FlowProjects · 1/1

Number Guessing Game

Part of the Python Roadmap

In short

Time to combine everything from this phase into a real game: the computer picks a secret number, you guess, and it says "too high" or "too low" until you win. It's a classic first project for good reason.

A game where the computer picks a random number and the player guesses until they get it, with 'too high' and 'too low' hints. It combines random, while loops, conditions and input handling.

The jaadu tiptoes across a cosy living room searching behind cushions while a friend on the sofa cups their hands and calls out hints.

Think of it like this

The hot-and-cold game

Remember hiding something and shouting "warmer" or "colder" as a friend searched? Each hint cuts down where it could be. Your game does the same with numbers: every "too high" or "too low" chops away part of the range until only the secret is left.

Watch it happen

One round of play (secret is 63)

Player
Game

Guess 1 to 100

1/6random.randint(1, 100) picked 63 and the loop starts.

The big ideas

What you need to know

random.randint(a, b)

Returns a random whole number from a to b, including both ends. Import the random module at the top.

while True + break

The game loops until the guess is correct, then break exits. You don't know in advance how many rounds it will take.

Validate before converting

text.isdigit() checks the input is all digits, so int() can't crash on "abc". continue asks again.

if / elif / else for hints

Too low, too high or correct: exactly one branch runs each round.

See it in code

Walk through the code

python
1import random2 3secret = random.randint(1, 100)4attempts = 05print("I'm thinking of a number between 1 and 100.")6 7while True:8    guess_text = input("Your guess: ").strip()9    if not guess_text.isdigit():10        print("Please type a whole number.")11        continue12 13    guess = int(guess_text)14    attempts += 115 16    if guess < secret:17        print("Too low!")18    elif guess > secret:19        print("Too high!")20    else:21        print(f"Correct! You got it in {attempts} tries.")22        break

1/5Import random, pick the secret and start the attempt counter at zero.

Save as guess.py and play. Using the middle-of-the-range strategy, you'll never need more than 7 guesses.

Save yourself the pain

Common mistakes

Oops

Calling random.randint inside the loop, so the secret changes every guess.

Do this instead

Pick the secret once, before the loop starts.

Oops

Converting with int(input()) directly and crashing on a typo.

Do this instead

Check with isdigit() first, or use try/except ValueError once you learn it.

Oops

Forgetting break after the correct guess, so the game never ends.

Do this instead

Always break (or update the loop condition) when the player wins.

Your mission

Level up the game

~40 min

Quick check

Why is random.randint(1, 100) called before the while loop instead of inside it?

Go deeper

  1. 1Use random.randint(1, 100) to choose the secret number.
  2. 2Count the attempts and print a different message for a quick win.
  3. 3Ask the player if they want to play again at the end.

Also part of these roadmaps

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge