LEARN Python - 3.5 Mini-Project – Guess the Number - zerotopyhero.com

Learn Python – 3.5: Mini-Project – Guess the Number

Your first real loop-powered Python game.

You’ve taught Python how to:

  • Make decisions

  • Repeat itself

  • Stop when it’s time

  • Skip things on purpose

  • Loop inside loops

That’s more than enough to build something that actually feels like a game.

So let’s do exactly that.

We’re building Guess the Number: a classic beginner game where Python secretly picks a number, and the user keeps guessing until they get it right.

Simple rules.
Clear logic.
Very satisfying win at the end.

What This Project Practices

This mini-project uses:

  • while loops

  • if / elif / else

  • Comparison operators

  • User input

  • break

  • Variables

  • Clean program flow

Nothing new.
Just everything working together.

Step 1: Pick the Secret Number

For now, we’ll keep it simple and hard-code the secret number.

(No randomness yet, that comes later.)

				
					# The secret number the user must guess
secret_number = 7

				
			

Python is now guarding a secret.
Very serious business.

Step 2: Ask for Guesses in a Loop

We want to keep asking the user until they guess correctly.

That means… a while loop.

				
					# Start the guessing loop
while True:
    guess = int(input("Guess the number (1–10): "))

				
			

This loop will run forever unless we stop it manually.

And we will.

Step 3: Compare the Guess

Now we decide what to do with each guess.

				
					   
    if guess < secret_number:
        print("Too low. Try again.")
    elif guess > secret_number:
        print("Too high. Try again.")
    else:
        print("You got it!")
        break

				
			

What’s happening here:

  • If the guess is too low → hint

  • If it’s too high → hint

  • Otherwise → correct → stop the loop

That break is the victory button.

Step 4: Celebrate the Win

Once the loop ends, Python continues normally.

				
					print("Game over. Thanks for playing!")

				
			

Full Program (Copy–Paste Ready)

Here’s the complete game:

				
					# The secret number
secret_number = 7

# Keep asking until the correct number is guessed
while True:
    guess = int(input("Guess the number (1–10): "))

    if guess < secret_number:
        print("Too low. Try again.")
    elif guess > secret_number:
        print("Too high. Try again.")
    else:
        print("You got it!")
        break

print("Game over. Thanks for playing!")
				
			

Run it.
Play it.
Win it.

You just built a real loop-driven game.

How This Game Thinks

Python’s logic looks like this:

  1. Ask for a guess

  2. Compare it

  3. Respond

  4. Repeat if needed

  5. Stop when correct

No magic.
Just logic and loops doing their job.

Mini Quiz

Try answering these:

  1. Why do we use while True here?

  2. What stops the loop from running forever?

  3. Which comparison checks for a correct guess?

  4. What happens if the user guesses correctly on the first try?

  5. What would happen if we removed break?

Extra Challenges (Optional)

Challenge A – Change the Number

Change secret_number to something else and test again.

Challenge B – Limited Attempts

Add a counter so the user only gets 5 guesses.

Challenge C – Better Messages

Print how many guesses it took when the user wins.

Challenge D – Custom Range

Let the user choose the range before guessing.

Step 4 Complete!

You’ve officially taught Python how to:

  • Loop

  • Decide

  • React

  • Stop intelligently

That’s a huge milestone.

ZeroToPyHero