ShareYourGroupShareYourGroupPython

Small project 1 of 4

Number guessing game

This is the classic first project. The computer picks a secret number and the player guesses until they find it, with a "too high" or "too low" hint after each try. It uses lessons 7 to 9, 17 and 19: input, if, while, the random module and try.

Plan before you type

Write the steps in plain words first. Programmers call this pseudocode.

  1. Pick a secret number from 1 to 20.
  2. Ask the player for a guess.
  3. If the guess is too high or too low, say so and ask again.
  4. If it is right, say how many tries it took and stop.

Steps 2 and 3 repeat an unknown number of times, so this is a while loop.

Step 1: one guess

Start small. Get one round working before adding the loop. The example fixes the random seed so that its output can be shown here. Remove that line in your own copy to get a new number every game.

import random
random.seed(3)

secret = random.randint(1, 20)
guess = int(input("Guess a number from 1 to 20: "))
if guess > secret:
    print("Too high")
elif guess < secret:
    print("Too low")
else:
    print("Correct!")

Output

Guess a number from 1 to 20: 10
Too high

Step 2: keep guessing

Wrap the question and the checks in while True and leave with break when the guess is right. Count the tries as you go. The player below typed 10, 5, 7 and 8.

import random
random.seed(3)

secret = random.randint(1, 20)
tries = 0
while True:
    guess = int(input("Your guess: "))
    tries += 1
    if guess > secret:
        print("Too high")
    elif guess < secret:
        print("Too low")
    else:
        print(f"Correct! You took {tries} tries.")
        break

Output

Your guess: 10
Too high
Your guess: 5
Too low
Your guess: 7
Too low
Your guess: 8
Correct! You took 4 tries.

Step 3: survive bad input

If the player types a word, int raises a ValueError and the game crashes. Catch it and ask again without counting a try. Also reject numbers outside the range. The player typed abc, 50, 10 and 8.

import random
random.seed(3)

LOW, HIGH = 1, 20
secret = random.randint(LOW, HIGH)
tries = 0
print(f"I am thinking of a number from {LOW} to {HIGH}.")
while True:
    try:
        guess = int(input("Your guess: "))
    except ValueError:
        print("Please type a whole number.")
        continue
    if not LOW <= guess <= HIGH:
        print(f"Stay between {LOW} and {HIGH}.")
        continue
    tries += 1
    if guess > secret:
        print("Too high")
    elif guess < secret:
        print("Too low")
    else:
        print(f"Correct! You took {tries} tries.")
        break

Output

I am thinking of a number from 1 to 20.
Your guess: abc
Please type a whole number.
Your guess: 50
Stay between 1 and 20.
Your guess: 10
Too high
Your guess: 8
Correct! You took 2 tries.

LOW and HIGH are written in capitals to show that they are settings that do not change while the program runs. Change them in one place and the whole game follows.

How the game is won

There is a best strategy: always guess the middle of what is left. Each hint then removes half of the remaining numbers. With 1 to 20, you never need more than five tries. With 1 to 100, never more than seven. This halving idea is called binary search, and computers use it to find things in sorted lists very quickly.

Ideas to extend it

Exercise 1. Add a limit of five tries. The game should print Out of tries and the secret number if the player does not find it. Test with the guesses 1, 2, 3, 4 and 5 and a secret of 9.

Show one answer
secret = 9
guesses = [1, 2, 3, 4, 5]   # in a real game these come from input()
for tries, guess in enumerate(guesses, 1):
    if guess == secret:
        print(f"Correct in {tries} tries")
        break
    print("Too low" if guess < secret else "Too high")
else:
    print("Out of tries. The number was", secret)

Output

Too low
Too low
Too low
Too low
Too low
Out of tries. The number was 9

The answer uses a rare feature: an else on a for loop runs only when the loop finished without a break.

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.