The big ideaA guessing game stores a secret and a guess, compares them with if, and loops.
How it works
A guessing game stores a secret and a guess, and compares them with if.
If the guess is less than the secret, the game says too low. Else if it is more, too high. Else, you win.
One equals sign stores a value in a name. Two equals signs check whether two things match.
A loop wraps around the check and repeats until the guess equals the secret.
One round of the game.
Example 1: Too low
The secret is seven and Teo guesses three.
Is three less than seven? Yes, so the game says Too low!
Example 2: Too high
Teo tries again with nine.
Nine is more than seven, so the game says Too high!
Example 3: A win
Teo guesses seven.
Seven equals seven, so the else path runs, and the game says You win!
Example 4: A whole game
A loop repeats the check until the guess equals the secret.
Teo tries five, then eight, then seven.
The game says Too low, then Too high, then You win.
Words to know
secret
The number the player has to find.
guess
The number the player tries.
elif
Else if: checked only when the first if was false.
loop
Repeats the check until the player wins.
Watch out
The player never sees the secret, or they could peek.
One equals sign stores, and two check. In if guess == secret:, two are needed.
The loop is what makes it a game. Without it, the check runs only once.
Plan first and code second: a secret, a guess and a check.
Try it
The secret is 7 and the guess is 5. What does the game say?
The secret is 7 and the guess is 8. What does the game say?
The secret is 7 and the guess is 7. What does the game say?
Which asks whether two things match: = or ==?
What keeps the game asking until the player wins?
Answers: 1. Too low · 2. Too high · 3. You win · 4. == · 5. A loop
Try at homeAsk a grown-up to think of a secret number from one to ten and keep it secret. You guess, and they tell you too low, too high, or you win. Count your guesses, and try to win in fewer next time.
For grown-upsPlay it unplugged first: pick a secret from one to ten, and say too low, too high or you win. Then ask your child to explain which line of the plan each reply came from.
Open these notes on screenScan the code, or go to tutorpip.com.