Python Number Guessing Game Project – Build a Game with Python

Introduction

The Number Guessing Game is one of the most popular beginner-friendly Python projects. It helps developers understand important programming concepts such as random number generation, loops, conditional statements, user input, and exception handling.

In this game, the computer randomly selects a number within a specified range, and the player attempts to guess it. After each guess, the program provides hints such as whether the guess is too high or too low until the correct number is found.

This project is simple, fun, and an excellent way to practice Python programming fundamentals.

Project Overview

The Number Guessing Game works as follows:

  1. The computer generates a random number.
  2. The player enters a guess.
  3. The program compares the guess with the secret number.
  4. The program provides feedback:
    • Too High
    • Too Low
    • Correct Guess
  5. The game continues until the player guesses correctly.

Features of the Project

  • Random number generation
  • User-friendly interaction
  • Unlimited guessing attempts
  • Hint system
  • Score tracking
  • Input validation
  • Replay option

Project Workflow


Start
 ↓
Generate Random Number
 ↓
Take User Guess
 ↓
Compare Guess
 ↓
Too High?
 ↓
Too Low?
 ↓
Correct?
 ↓
Display Result
 ↓
End

Algorithm

  1. Import the random module.
  2. Generate a random number.
  3. Ask the user to enter a guess.
  4. Compare the guess with the secret number.
  5. Display a hint.
  6. Repeat until the correct number is guessed.
  7. Display the number of attempts.
  8. End the game.

Basic Number Guessing Game

Source Code


import random
secret_number = random.randint(1, 100)
while True:
    guess = int(
        input("Enter Your Guess: ")
    )
    if guess > secret_number:
        print("Too High!")
    elif guess < secret_number:
        print("Too Low!")
    else:
        print(
            "Congratulations! You guessed correctly."
        )
        break

Output:

Enter Your Guess: 50
Too Low!
Enter Your Guess: 75
Too High!
Enter Your Guess: 63
Congratulations! You guessed correctly.

Understanding the Code

Generate Random Number


secret_number = random.randint(1, 100)

This generates a random number between 1 and 100.

Example:


63

Take User Input


guess = int(input())

The user enters a number.

Compare Values


if guess > secret_number:

Checks whether the guess is greater than the secret number.

Exit the Loop


break

Stops the game when the user guesses correctly.

Number Guessing Game with Attempt Counter

Tracking attempts makes the game more interesting.

Source Code:


import random
secret_number = random.randint(1, 100)
attempts = 0
while True:
    guess = int(
        input("Enter Guess: ")
    )
    attempts += 1
    if guess > secret_number:
        print("Too High!")
    elif guess < secret_number:
        print("Too Low!")
    else:
        print(
            "Correct Guess!"
        )
        print(
            "Attempts:",
            attempts
        )
        break

Output:

Enter Guess: 40
Too Low!
Enter Guess: 80
Too High!
Enter Guess: 65
Correct Guess!
Attempts: 3

Number Guessing Game with Limited Attempts

This version adds difficulty.

Source Code


import random
secret_number = random.randint(1, 50)
max_attempts = 5
for attempt in range(max_attempts):
    guess = int(
        input("Enter Guess: ")
    )
    if guess == secret_number:
        print(
            "You Win!"
        )
        break
    elif guess > secret_number:
        print("Too High!")
    else:
        print("Too Low!")
else:
    print(
        "Game Over!"
    )
    print(
        "Number was:",
        secret_number
    )

Difficulty Levels

You can create multiple levels.

Level Range
Easy (1–10)
Medium (1–50)
Hard (1–100)
Expert (1–500)

Example:


secret_number = random.randint(1, 500)

Number Guessing Game Using Functions

Functions improve code organization.

Source Code


import random
def play_game():
    number = random.randint(1, 100)
    while True:
        guess = int(
            input(
                "Guess Number: "
            )
        )
        if guess > number:
            print("Too High")
        elif guess < number:
            print("Too Low")
        else:
            print("Correct!")
            break
play_game()

Input Validation Using Exception Handling

Users may enter invalid values.

Example:


try:
    guess = int(
        input(
            "Enter Number: "
        )
    )
except ValueError:
    print(
        "Please enter a valid number."
    )

This prevents program crashes.

Advanced Number Guessing Game

Features:

  • Difficulty levels
  • Score system
  • Limited attempts
  • Replay option
  • High score tracking

Example:


score = 100 - attempts * 10

The fewer attempts, the higher the score.

Real-Life Applications

Although simple, this project demonstrates concepts used in:

Gaming Applications

Player interaction and game logic.

Educational Software

Learning and quiz applications.

AI Training Simulations

Decision-making exercises.

Puzzle Games

Logic-based gaming systems.

Common Errors and Solutions

1. Forgetting to Import Random Module

Incorrect:


random.randint(1, 100)

Error:

NameError

Solution:


import random

2. Invalid User Input

Incorrect:


abc

Error:

ValueError

Solution:


try:
    pass
except:
    pass

3. Infinite Loop

Incorrect:


while True:

without a break statement.

Always include:


break

when the correct number is guessed.

Conclusion

The Python Number Guessing Game is an excellent beginner project that combines fun with learning. It helps developers practice random number generation, loops, conditional statements, functions, and exception handling while building an interactive application.

As your skills grow, you can enhance the project with scoring systems, difficulty levels, graphical interfaces, and multiplayer features. Completing this project strengthens your Python fundamentals and prepares you for more advanced programming challenges.

Related Python Tutorials