Skip to content

Project: Number Guessing Game

Build a number guessing game that gives feedback and counts each attempt.

18 min read 10 quiz questions

Stage 1: Pick a secret number

Our game needs a secret number. The random module can choose one, but we call random.seed(7) first so this lesson always gives the same result.

A real game would use input(). This app cannot pause for interactive input, so we will drive the game with a list of guesses written in the code.

Stage 1: Choose a repeatable secret
import random

random.seed(7)
secret = random.randint(1, 10)
print(f"Secret picked: {secret}")

The seed makes the random choice repeatable for this example. The secret is a number from 1 through 10.

Stage 2: Compare one guess

Start with one guess and compare it with the secret. An if statement lets the program choose a helpful message.

Stage 2: Give higher or lower feedback
secret = 6
guess = 4

print(f"Guess: {guess}")
if guess < secret:
    print("Go higher")
elif guess > secret:
    print("Go lower")
else:
    print("Correct!")

Because 4 is less than 6, the program prints Go higher. The other branches handle a guess that is too high or exactly right.

Stage 3: Check a list with a while loop

A list gives the game several prepared guesses. A while loop uses an index to visit one guess at a time and increases the attempt counter.

Stage 3: Loop through prepared guesses
secret = 6
guesses = [2, 8, 6]
attempts = 0
index = 0

while index < len(guesses):
    guess = guesses[index]
    attempts += 1
    print(f"Attempt {attempts}: {guess}")
    if guess < secret:
        print("Go higher")
    elif guess > secret:
        print("Go lower")
    else:
        print("Correct!")
        break
    index += 1

The loop stops early with break when it finds the secret. attempts records how many guesses were checked.

Stage 4: Finish the game

Now combine the seeded secret, prepared guesses, feedback, and final attempt total into one small program.

Stage 4: Complete number guessing game
import random

random.seed(7)
secret = random.randint(1, 10)
guesses = [2, 8, 6]
attempts = 0
index = 0

print("Number Guessing Game")
while index < len(guesses):
    guess = guesses[index]
    attempts += 1
    print(f"Attempt {attempts}: {guess}")
    if guess < secret:
        print("Go higher")
    elif guess > secret:
        print("Go lower")
    else:
        print("Correct!")
        break
    index += 1

if index == len(guesses):
    print("No more guesses.")
print(f"Attempts used: {attempts}")

The complete game checks every prepared guess until one is correct. It then reports the number of attempts used.

Key points

  • random.seed() makes a random example repeatable.
  • A list can stand in for interactive guesses in a runnable lesson.
  • if, elif, and else choose higher, lower, or correct feedback.
  • A while loop can process guesses one at a time.
  • A counter tracks the number of attempts.

Worked example

The classic guessing game made test-friendly: seed the random source so the secret is identical every run while learning.

Example
import random

rng = random.Random(42)
secret = rng.randint(1, 10)
guesses = [5, secret]
attempts = 0
for guess in guesses:
    attempts += 1
    if guess == secret:
        break
print(secret, attempts)

How it works, step by step

  1. random.Random(42) is a seeded generator — same sequence everywhere, unlike random.randint directly.
  2. The loop stands in for a player: keep guessing until the secret matches.
  3. attempts counts turns taken, the score of the game.

References & Further Reading

Related entries from PyLabs's own reference library.

  • input() (Read a line of text typed by a user.)
  • int() (Convert a value to an integer.)
  • print() (Print objects to the text stream file, separated by sep and followed by end.)

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Practice exercise

Not passed yet

Make a deterministic guessing check. Set secret to 5, use guesses = [3, 5], and count guesses in attempts. Print each guess as Guess: number. When the secret is found, print Correct!, then print Attempts: 2.

# Create the secret and the list of guesses
# Use a while loop to check the guesses
Want another challenge on this module? More coding practice

Check yourself

Project: Number Guessing Game quiz

10 questions. You get instant feedback per question, and the timer starts when you press the button.

Lesson incomplete

Not completed

Create an ID or sign in to save lesson completion across devices.

Create ID to save
Module lesson progress0/4 complete
Module cheat sheet