Python From Scratch #30 - Project 4 (Number Guessing Game)
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:
Picks a random number between 1 and 100.
Asks the user to guess the number.
Tells the user if their guess is too high, too low, or correct.
Keeps asking until the user guesses correctly.
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 randomandrandom.randint()to pick the numberinput()andint()to take each guessComparison operators (
<,>,==)if,elif,elseto respond to the guessA
while Trueloop withbreakto keep asking until the correct guessA 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 Truecombined withbreakworks well here because you don’t know in advance how many guesses it will take.Start a counter at
0before the loop, and add1to it on every guess.Compare the guess to the number using
if/elif/else, just like we did with the Grade Calculator.Use
breakonly 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.")
breakHow It Works
random.randint(1, 100)picks the number once, before the loop starts, so it stays the same for the whole game.guessesstarts at0and increases by1on every guess using+=from the operators lesson.while Truekeeps the loop running untilbreakstops it.The
if/elif/elseblock checks the guess against the random number and gives the user feedback.breakonly runs inside theelseblock 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!")
breakThis 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!")
breakIndentation 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.
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 ☕
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