Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Projects
  3. Build a Number Guessing Game in Python

Project 1 · Guided build

Build a Number Guessing Game in Python

Create a complete guessing game with random secrets, limited attempts, directional hints, and a replay loop.

Open the compilerWarm-up exercises
beginner45–60 minutesrandom modulewhile loopsinput validationf-strings
On this page
  1. What you will build
  2. Requirements
  3. Plan the program
  4. Starter code
  5. Make it your own
  6. Testing checklist
  7. Stretch goals

A guessing game is a compact project that exercises input, conditions, loops, and random numbers. You will build it incrementally, then test the edge cases.

What you will build#

The program chooses a secret number between 1 and 100. The player has a limited number of attempts. After each guess, the program reports whether the guess is too low, too high, or correct. The game ends with a win or a loss message.

Requirements#

  1. Choose a random integer from 1 to 100 with random.randint(1, 100).
  2. Allow at most 7 guesses.
  3. Print Too low, Too high, or Correct after each guess.
  4. Reject values outside 1–100 without consuming an attempt? Decide and document your choice.
  5. After the game, print the secret number if the player lost.
  6. Validate input with try/except ValueError instead of crashing.

Plan the program#

Write the loop in plain language before coding:

  • Set secret and attempts_left.
  • While attempts remain:
    • Read a guess.
    • Validate it.
    • Compare it with the secret.
    • Stop on a correct guess.
  • Print the result.

Keep input and game logic separate where possible. A function such as read_guess() can return a valid integer or None, leaving the loop focused on the game.

Starter code#

Python
import random

secret = random.randint(1, 100)
max_attempts = 7
attempts = 0

print("Guess a number between 1 and 100.")
print(f"You have {max_attempts} attempts.")

while attempts < max_attempts:
    text = input()
    attempts += 1

    try:
        guess = int(text)
    except ValueError:
        print("Enter a whole number.")
        continue

    if guess < 1 or guess > 100:
        print("Stay between 1 and 100.")
        continue

    if guess < secret:
        print("Too low")
    elif guess > secret:
        print("Too high")
    else:
        print(f"Correct in {attempts} attempts!")
        break
else:
    print(f"Out of attempts. The number was {secret}.")

Make it your own#

  • Change the range and the attempt limit.
  • Print how many attempts remain after each guess.
  • Use a while True loop with an explicit break instead of the loop condition.
  • Track previous guesses in a list and reject duplicates with a helpful message.
  • Provide a difficulty menu that sets the range and attempts before the game starts.

Testing checklist#

Test these cases with the Input panel:

| Scenario | Input | Expected behaviour | | --- | --- | --- | | Correct first guess | The secret value | Correct in 1 attempts! | | Too low then correct | A lower value, then the secret | Too low then success | | Non-number | abc | Validation message, run continues | | Out of range | 0 or 101 | Range message, run continues | | All attempts used | Seven wrong values | Loss message with the secret |

Because ILoveCompiler supplies all input before the run, the secret is chosen fresh each run. Use the printed hints to adapt your next guess, or temporarily print the secret while testing.

Seed the random number for repeatable tests

random.seed(42) makes the secret predictable for a test run. Remove the seed when you want a real game.

Stretch goals#

Add a score based on attempts remaining, keep a high-score table in a dictionary for the current session, or allow the player to choose the maximum number. Keep each addition small and test it before moving on.

Next projectBuild a Command-Line Todo List in Python

On this page

  1. What you will build
  2. Requirements
  3. Plan the program
  4. Starter code
  5. Make it your own
  6. Testing checklist
  7. Stretch goals

Project checklist

  • Read the requirements.
  • Plan the data model.
  • Build one feature at a time.
  • Test invalid input.
  • Refactor after it works.