Skip to content

Guess the number

Make a game where the computer thinks of a number, and you keep guessing until you get it.

Start here: open pygwide. On the home page, choose Tutorials, then Python Basics, then Lesson 5: Guess the number.

This lesson takes about an hour. The extensions take about 30 minutes more.

What you will learn

  • How to repeat lines with a while loop.
  • How to stop a loop that never ends.
  • How to count things as they happen.

What you need from before

  • int(input()), from Lesson 2.
  • if, elif, else and comparing with != and <, from Lesson 3.
  • random.randint(), from Lesson 4.

Step 1: Count to five

A while loop repeats its lines while something is true. Type this and run it:

count = 1
while count <= 5:
    print(count)
    count = count + 1
print("Done!")

Python checks count <= 5. It is true, so it runs the indented lines, then goes back and checks again. When count reaches 6, the check is false, so the loop ends and Python carries on with print("Done!").

In Scratch

This is like repeat until, but the other way round. Scratch repeats until something is true. Python repeats while something is true.

Step 2: A loop that never ends

What happens if you forget count = count + 1? The count never changes, so the loop goes on for ever. Try it, then press ■ Stop.

If a program of yours ever seems stuck, check your loop, and press Stop.

Step 3: The secret number

Replace your code with the start of the game. The computer picks a secret number, and the loop keeps asking until your guess is right:

import random

secret = random.randint(1, 100)
guess = 0

print("I am thinking of a number between 1 and 100.")

while guess != secret:
    guess = int(input("Your guess: "))

print("You got it!")

!= means "is not the same as". We set guess to 0 first, so the loop starts: 0 is never the secret number.

Step 4: Give hints

It's hard to guess without help! Add hints inside the loop, under the input() line, indented the same:

    if guess < secret:
        print("Too low!")
    elif guess > secret:
        print("Too high!")

Step 5: Count the guesses

Make a guesses variable that starts at 0, next to guess = 0. Add 1 to it each time round the loop, and say how many guesses it took at the end:

print(f"You got it in {guesses} guesses!")

Check your code

# Guess the number
import random

secret = random.randint(1, 100)
guesses = 0
guess = 0

print("I am thinking of a number between 1 and 100.")

while guess != secret:
    guess = int(input("Your guess: "))
    guesses = guesses + 1
    if guess < secret:
        print("Too low!")
    elif guess > secret:
        print("Too high!")

print(f"You got it in {guesses} guesses!")

Extensions

Only seven guesses

Give the player only 7 guesses. and lets you check two things at once: the loop keeps going while the guess is wrong and there are guesses left.

while guess != secret and guesses < 7:
    guess = int(input("Your guess: "))

After the loop, use if to say whether they won or ran out of guesses.

Then ask "Play again?", and wrap the whole game in another loop that repeats while the answer is "yes". .lower() turns the answer into small letters, so "YES" works too.

One way to do it
# Guess the number, with 7 guesses and another go
import random

again = "yes"
while again == "yes":
    secret = random.randint(1, 100)
    guesses = 0
    guess = 0
    print("I am thinking of a number between 1 and 100.")
    print("You have 7 guesses.")

    while guess != secret and guesses < 7:
        guess = int(input("Your guess: "))
        guesses = guesses + 1
        if guess < secret:
            print("Too low!")
        elif guess > secret:
            print("Too high!")

    if guess == secret:
        print(f"You got it in {guesses} guesses!")
    else:
        print(f"Out of guesses! It was {secret}.")

    again = input("Play again? (yes or no) ").lower()

print("Thanks for playing!")

Bigger numbers

Change the game to pick a number from 1 to 1000. How many guesses do you need now?

Extra bits

The best way to guess

Always guess the number halfway between the lowest and highest it could be. Each guess then cuts the numbers left in half. With this trick, you never need more than 7 guesses for a number from 1 to 100!

Export your project

pygwide keeps your work in this web browser, but club computers can forget it. Press ⇩ Export to save your project as a zip file. Keep it somewhere safe, such as a USB stick or your school drive.

Next time, choose ⇪ Import a project on the home page to carry on with it.