Table of Contents
Open Table of Contents
Before you start 🧭
✅ Finish lesson 0 and
lesson 1 first—you will reuse
input(), print(), and running a .py file.
✅ A grown-up nearby helps for the terminal and for reading errors if something surprises you.
✅ Install notes and uv tips live in the companion
SETUP.md. Stuck? Reference code is under
lessons/02-guess-the-number/solution/ in the
companion lab repository.
🎯 Pro tip: type the small programs yourself. Your fingers learn the patterns.
Goals 🎯
By the end of this lesson you will:
- 🔁 Run a
whileloop until a win condition - 🌿 Branch with
if,elif, andelse - 🎲 Pick a random secret with
random.randint - 🔢 Turn text into a number with
int(...)aroundinput()
Why this is “game dev” 🎮
Big games repeat a core loop: read what the player did, decide what it means, update the world, show feedback—then do it again. Your guess-the-number program is a tiny version of that loop: read a guess, compare, print a clue, repeat until “you win.”
Mini project: secret number 🔮
You will build guess.py in your lesson folder. The computer picks a secret
number; you guess until you get it.
Step 1 — pick a secret with random 🎲
Create guess.py. Add:
import random
secret = random.randint(1, 10)
print("I picked a secret number from 1 to 10.")
randint(1, 10) means “pick an integer including both ends.” Each run can
pick a different secret.
What is import? (click or tap)
import random means “load Python’s random toolkit.” That
toolkit knows how to shuffle cards, pick random integers, and more. We only need
randint here.
Run the file once. You should see the message (you are not guessing yet). ✅
Step 2 — one guess as a number 🔢
input() always returns text (a string). Compare numbers, so convert:
import random
secret = random.randint(1, 10)
print("I picked a secret number from 1 to 10.")
guess_text = input("Your guess: ")
guess = int(guess_text)
print("You guessed:", guess)
int(...) turns text like "7" into the number 7.
Honest heads-up: if the player types letters or a blank line, int can
crash with an error. For this lesson, type digits only (ask a grown-up to
read the error if it happens). Later you can add friendlier checks as a stretch.
Why not compare text to a number?
The secret is a number. If you forget int, you might compare
text to a number—and Python will complain, or you might get surprising results.
Converting the guess keeps the rules clear.
Step 3 — too low, too high, or just right 🌿
Add branching below your guess line:
if guess < secret:
print("Too low!")
elif guess > secret:
print("Too high!")
else:
print("You got it!")
ifruns its block when the condition is true.elif(“else if”) tries the next condition when the ones above failed.elseruns when none of the earlier conditions matched.
Try a wrong guess on purpose, then a correct guess. Does the message match?
Step 4 — loop until the win 🔁
Wrap the “ask + compare” part in a while loop:
import random
secret = random.randint(1, 10)
print("I picked a secret number from 1 to 10.")
while True:
guess_text = input("Your guess: ")
guess = int(guess_text)
if guess < secret:
print("Too low!")
elif guess > secret:
print("Too high!")
else:
print("You got it! Nice job.")
break
while True means “keep going.” break means “stop the loop now”—we
only break when the guess is correct.
Play a few rounds. Celebrate when you win. 🎉
Try this 🛠️
- Change the range — use
randint(1, 20)and update the printed message so the player knows the new high end. - Count guesses — make a variable like
tries = 0, add1each loop, and print how many tries it took when they win. - Flavor text — rewrite “Too low!” / “Too high!” as silly taunts from a grumpy oracle (keep it kind and inclusive).
Stretch goals 🌟
- Max tries — stop the game with a different message if the player uses too many guesses without finding the secret.
- Play again — after a win (or loss), ask “Play again? (y/n)” and start a new secret when they say yes.
- Safer input — if you are ready for a grown-up-assisted detour, wrap
int(...)intry/exceptso bad typing prints a friendly “digits only” message instead of a scary traceback.
🙈 Spoiler direction for “play again”
Use another while True around the whole round, and break
out when the player says they are done. Reset secret each new round.
Recap 📝
random.randintpicks the secret.int(input(...))turns the player’s text into a number for comparisons.if/elif/elsechoose the right feedback.while+breakkeep the game going until someone wins.
Next lesson (preview) 🔮
Next you will lean on strings and choices to build a small text adventure—menus, rooms, and story branches—still in the terminal, still very much “game shaped.”