←Previous page

12GAME PROJECTS · GUESS THE NUMBER

Build a guessing game.

PyBot hides a number from 1 to 10. You guess, and it says higher or lower until you get it.

IN REAL LIFE

Guessing games are everywhere.

Someone keeps a secret and gives clues until you find it.

How many fingers?

A friend hides a hand. You guess, they say "more" or "less".

Hot and cold

You look for a hidden toy. "Warmer!" means you are getting close.

Price quiz shows

Players guess a price and hear "higher" or "lower" each time.

THE PLAN

The game in four small steps.

Each step uses something you already know.

Hide a secret

Python picks a number from 1 to 10 and keeps it in a box.

random.randint

Ask for a guess

Ask the player and turn the answer into a number.

input + int

Give a hint

Compare the guess with the secret: higher or lower?

if / elif

Repeat and count

Keep asking until the guess is right. Count every try.

while + counter

RUN IT

Play the whole game.

Guess first. Then run real Python and check.

1

Guess the result

PyBot's answers try all 10 numbers, one per line.

The secret changes each run. How does every run end?

guess.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change randint(1, 10) to randint(1, 3). Leave only 1, 2 and 3 in the answers.

BUILD IT

Build it step by step.

PyBot fixed the secret to 7 to test the game. Now every run gives the same result.

0Completed
11Not tried
0Review
01

Step 1: one guess, one hint

Python can't compare the text "4" with the number 7. Fix the line that asks.

Goal output
Your guess (1-10)? 4
Higher!
OUTPUT

02

Step 2: the hints in a loop

The loop works, but the hints point the wrong way. 4 is less than 7, so the hint must be Higher!

Goal output
Your guess (1-10)? 4
Higher!
Your guess (1-10)? 9
Lower!
Your guess (1-10)? 7
You got it!
OUTPUT

03

Step 3: count the tries

Three guesses, but PyBot says Tries: 1. Move one line so the counter keeps adding.

Goal output
Your guess (1-10)? 4
Your guess (1-10)? 9
Your guess (1-10)? 7
You got it!
Tries: 3
OUTPUT

YOUR TURN

Questions about the game.

Try each tiny question. A wrong answer becomes something to review.

04

Which secrets?

secret = random.randint(1, 10)

Which numbers can the secret be?

05

Why int()?

guess = int(input("Your guess? "))

Why is input() inside int()?

06

When does it stop?

while guess != secret:

When does this loop stop asking?

07

Which hint?

secret = 7 guess = 9 if guess < secret: print("Higher!") elif guess > secret: print("Lower!")

What will Python show?

08

Count the tries

tries = tries + 1

The secret is 7. The player types 5, then 8, then 7.

What is in tries at the end?

09

The first lap

guess = 0 while guess != secret:

Why does guess start at 0?

10

Testing trick

To test the game, PyBot writes secret = 7 instead of random.randint(1, 10).

Why does this help?

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A game is small steps joined.

random hides the secret, input and int ask, if gives hints, and while repeats. Test with a fixed secret first.