Kids Coding
Project: Number Guess
The computer picks a number. You guess. if tells you too high or too low.
KidsBasicProject: Number Guess
Too high, too low
The computer keeps a secret number. You guess. if says whether you won, went too high, or went too low.
One guess
secret = 7
guess = 4
if guess == secret:
print("You got it")
elif guess < secret:
print("Too low")
else:
print("Too high")elif means else if
When there are three paths, use if, then elif, then else. Python picks the first yes and skips the rest.
Try more than one guess
Here the guesses are saved in the program so it runs in the editor. Change guess and run again.
Change this number
secret = 7
guess = 7
print("You guessed", guess)
if guess == secret:
print("Yes! It was", secret)
elif guess < secret:
print("Too low. Try a bigger number.")
else:
print("Too high. Try a smaller number.")Later, Intermediate will add random and a while loop so the secret changes and you can guess until you hit it.
Real life
A jar of sweets at a party. How many inside?
You guess. Someone says too high or too low. if is that voice.
Sweets in the jar
jar = 18
guess = 12
print("You guessed", guess)
if guess == jar:
print("You win the jar")
elif guess < jar:
print("Too low — there are more")
else:
print("Too high — fewer than that")Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Set secret to 3 and guess to 10. Print Too high using if / else.
Show solution
secret = 3
guess = 10
if guess == secret:
print("Yes")
else:
print("Too high")