mailwal

Python From Scratch #30 - Project 4 (Number Guessing Game)

Shefali Jangid

Python From Scratch #30 - Project 4 (Number Guessing Game)

Build an interactive number guessing game using loops, conditionals, and operators.

Hey 👋

Welcome back to the Python from Scratch series.

No new topic today. Instead, we’ll build our fourth project: a number guessing game using everything we’ve learned about operators, conditionals, and loops.

The Task

Build a program that:

  1. Picks a random number between 1 and 100.

  2. Asks the user to guess the number.

  3. Tells the user if their guess is too high, too low, or correct.

  4. Keeps asking until the user guesses correctly.

  5. Counts and prints how many guesses it took.

A Quick New Thing: import

We need a random number for this project, so use the following:

import random

number = random.randint(1, 100)

This picks a random whole number between 1 and 100, including both numbers.

Don’t worry about understanding import or random yet. We’ll learn about modules and import later in the series.

For now, just use these two lines to generate the random number.

What You’ll Use

  • import random and random.randint() to pick the number

  • input() and int() to take each guess

  • Comparison operators (<, >, ==)

  • if, elif, else to respond to the guess

  • A while True loop with break to keep asking until the correct guess

  • A counter variable to track the number of guesses

Expected Output

Guess the number between 1 and 100: 50
Too high! Try again.
Guess the number between 1 and 100: 25
Too low! Try again.
Guess the number between 1 and 100: 37
Correct! You guessed it in 3 tries.

Try It Yourself First

Here are a few hints:

  • while True combined with break works well here because you don’t know in advance how many guesses it will take.

  • Start a counter at 0 before the loop, and add 1 to it on every guess.

  • Compare the guess to the number using if/elif/else, just like we did with the Grade Calculator.

  • Use break only when the guess is correct.

Give it a try before you scroll down.

Solution

import random

number = random.randint(1, 100)
guesses = 0

while True:
    guess = int(input("Guess the number between 1 and 100: "))
    guesses += 1

    if guess < number:
        print("Too low! Try again.")
    elif guess > number:
        print("Too high! Try again.")
    else:
        print("Correct! You guessed it in", guesses, "tries.")
        break

How It Works

  • random.randint(1, 100) picks the number once, before the loop starts, so it stays the same for the whole game.

  • guesses starts at 0 and increases by 1 on every guess using += from the operators lesson.

  • while True keeps the loop running until break stops it.

  • The if/elif/else block checks the guess against the random number and gives the user feedback.

  • break only runs inside the else block because that’s the only case where the guess is correct.

Common Mistake

Be careful where you place break.

For example:

while True:
    guess = int(input("Guess the number between 1 and 100: "))
    guesses += 1

    if guess < number:
        print("Too low! Try again.")
    elif guess > number:
        print("Too high! Try again.")
    else:
        print("Correct!")

    break

This stops the loop after the first guess, whether the guess is correct or not.

Because break is now outside the else block.

It runs every time the loop reaches that line.

The correct version keeps break inside else:

else:
    print("Correct!")
    break

Indentation decides which block a line belongs to, just like we saw in the nested if lesson.

Challenges

If you want to go further, try these:

  • Give the player a maximum of 10 guesses. If they use all 10 guesses without finding the number, end the game.

  • If the player runs out of guesses, show them the correct number.

POLL
Is this Python series helping you learn? Should I continue it?

That’s all for today!

Tomorrow, we’ll start learning about functions in Python.


If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️


Want to sponsor my work? Let’s talk!

See you tomorrow ✨

- Shefali

Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.

If you enjoyed this edition of DevBits, you can support my work here ☕

Buy Me a Coffee

Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.

© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104