Track: Making Games · Lesson 2 of 10 · Ages: 8+ · Time: 40 minutes
Beep is thinking of a secret number. Can you find it? You get a clue after every guess: “too high” or “too low”. It is one of the oldest and best games to program, and you will learn three big ideas on the way: random numbers, the while loop and comparing numbers.
Beep says I am thinking of a number from 1 to 10. It is a very good number. Beep boop!
What you need
- A computer with Python 3 and IDLE
- Your first Python lessons done (variables,
print,if) - About 40 minutes
Grown-ups The program uses input(), which always gives back text, so we turn it into a number with int(). If your child types a word instead of a number the program will stop with an error. That is a great moment to talk about why real games check what a player types. We keep the first version simple on purpose.
Step 1: let the computer pick a secret
Python has a helper called random that can pick numbers for us. We have to say import random first. Then random.randint(1, 10) picks a whole number from 1 to 10, and it can pick 1 or 10 too.
Step 2: keep asking until the guess is right
A loop repeats code. You have used for loops that repeat a set number of times. A while loop is different: it keeps repeating while something is true, and stops when it becomes false. We do not know how many guesses you will need, so while is perfect.
Here is the whole game. Read it carefully before you run it.
import random
random.seed(7)
secret = random.randint(1, 10)
print("I am thinking of a number from 1 to 10.")
guess = 0
tries = 0
while guess != secret:
guess = int(input("Your guess? "))
tries = tries + 1
if guess < secret:
print("Too low!")
elif guess > secret:
print("Too high!")
print("You got it in", tries, "tries!")
When you play, it looks like this (the numbers after the question marks are what you type):
I am thinking of a number from 1 to 10. Your guess? 5 Too low! Your guess? 8 Too high! Your guess? 7 Too high! Your guess? 6 You got it in 4 tries!
Here is what the important lines do:
guess != secretis a comparison. The!=sign means “is not equal to”. The loop keeps going while the guess is not the secret.int(input(...))asks the question, then turns the typed text into a whole number so we can compare it.guess < secretasks “is the guess smaller than the secret?” andguess > secretasks “is it bigger?”.tries = tries + 1adds one to the counter after each guess.elifmeans “otherwise, if”. It is a second question the computer only asks if the first answer was no.

Pixel says Did you spot random.seed(7)? It makes the “random” number the same every time. That is how we can check the game works. Meow!
What is a seed?
A computer cannot really roll dice in its head. It follows a clever recipe that makes numbers that look random. A seed is the starting point of that recipe. If you start from the same seed, you get the same numbers in the same order, every single time. With random.seed(7) the secret here is 6.
That is handy while you build and test a game, because you can predict what will happen. But it is no fun to play, because the secret never changes! Once your game works, delete the random.seed(7) line and the secret will be different each time. (On some computers the same seed can pick a different number. If your secret is not 6, your hints will look different, and that is fine.)
Step 3: a limit on guesses
A game gets more exciting when it can be lost. Let us give the player only five guesses. We use a variable called won that is False until the player gets it right.
import random
secret = random.randint(1, 10)
guesses_left = 5
won = False
print("I am thinking of a number from 1 to 10. You have 5 guesses.")
while guesses_left > 0 and not won:
guess = int(input("Your guess? "))
guesses_left = guesses_left - 1
if guess == secret:
won = True
print("You got it!")
elif guess < secret:
print("Too low!")
else:
print("Too high!")
if not won:
print("Out of guesses! My number was", secret)
The loop now has two reasons to stop, joined with and: the guesses run out, or the player wins. The loop only keeps going while both guesses_left > 0 and not won are true.
Your game might look like this if the secret happens to be 6:
I am thinking of a number from 1 to 10. You have 5 guesses. Your guess? 5 Too low! Your guess? 8 Too high! Your guess? 6 You got it!
Did you know? There is a clever trick to win this game almost every time. Always guess the number in the middle of the numbers that are left. Each answer cuts the possibilities in half. Computer scientists call this a binary search, and real programs use it to find things quickly.
Challenges
Try it Change the game so the secret is between 1 and 100. How many guesses do you think you need if you always guess the middle? Try it with a friend picking the number.
Show a hint
Start with 50. If the answer is “too low”, the secret is between 51 and 100, so guess 75 next. Each guess cuts the choices in half. Seven guesses are always enough for numbers from 1 to 100, because 2 x 2 x 2 x 2 x 2 x 2 x 2 makes 128, which is more than 100.
Try it Add a special message if the player wins on the very first try.
Show the answer
Add a check after the loop. This version uses the seed so that the checker can win on the first try with a guess of 6.
import random
random.seed(7)
secret = random.randint(1, 10)
print("I am thinking of a number from 1 to 10.")
guess = 0
tries = 0
while guess != secret:
guess = int(input("Your guess? "))
tries = tries + 1
if guess < secret:
print("Too low!")
elif guess > secret:
print("Too high!")
if tries == 1:
print("WOW! First try! Are you a mind reader?")
else:
print("You got it in", tries, "tries!")
What you learned
random.randint(1, 10)picks a random whole number, andrandom.seed(...)makes it repeatable for testing.- A while loop repeats for as long as something is true.
<,>,==and!=compare values.- A game needs a way to be won and a way to be lost.
