How many fingers?
A friend hides a hand. You guess, they say "more" or "less".
12GAME PROJECTS · GUESS THE NUMBER
PyBot hides a number from 1 to 10. You guess, and it says higher or lower until you get it.
IN REAL LIFE
Someone keeps a secret and gives clues until you find it.
A friend hides a hand. You guess, they say "more" or "less".
You look for a hidden toy. "Warmer!" means you are getting close.
Players guess a price and hear "higher" or "lower" each time.
THE PLAN
Each step uses something you already know.
Python picks a number from 1 to 10 and keeps it in a box.
random.randint
Ask the player and turn the answer into a number.
input + int
Compare the guess with the secret: higher or lower?
if / elif
Keep asking until the guess is right. Count every try.
while + counter
RUN IT
Guess first. Then run real Python and check.
PyBot's answers try all 10 numbers, one per line.
The secret changes each run. How does every run end?
PyBot's hint
Ready. Press Run Python.
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
PyBot fixed the secret to 7 to test the game. Now every run gives the same result.
Python can't compare the text "4" with the number 7. Fix the line that asks.
Your guess (1-10)? 4 Higher!OUTPUT
The loop works, but the hints point the wrong way. 4 is less than 7, so the hint must be Higher!
Your guess (1-10)? 4 Higher! Your guess (1-10)? 9 Lower! Your guess (1-10)? 7 You got it!OUTPUT
Three guesses, but PyBot says Tries: 1. Move one line so the counter keeps adding.
Your guess (1-10)? 4 Your guess (1-10)? 9 Your guess (1-10)? 7 You got it! Tries: 3OUTPUT
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
secret = random.randint(1, 10)Which numbers can the secret be?
guess = int(input("Your guess? "))Why is input() inside int()?
while guess != secret:When does this loop stop asking?
secret = 7
guess = 9
if guess < secret:
print("Higher!")
elif guess > secret:
print("Lower!")What will Python show?
tries = tries + 1The secret is 7. The player types 5, then 8, then 7.
What is in tries at the end?
guess = 0
while guess != secret:Why does guess start at 0?
Why does this help?
Want a fresh start? Clear only this page.
random hides the secret, input and int ask, if gives hints, and while repeats. Test with a fixed secret first.