Build a Number Guessing Game Python Tutorial: Step-by-Step Code Guide

Logan Jul 13, 2026

The thrill of a number guessing game can be easily replicated on your computer using Python, a popular programming language. This tutorial will guide you through creating an interactive number guess game from scratch, making it a perfect project for intermediate programmers or those learning Python for the first time.

Simple, Quick, and Fun Number Guessing Python Game for Beginners
Simple, Quick, and Fun Number Guessing Python Game for Beginners

By the end of this tutorial, you'll be able to create a number guessing game that generates a random number for the player to guess. The game will provide feedback, letting the player know if their guess is correct, too high, or too low. Let's dive into creating this engaging game using Python.

Number guessing game
Number guessing game

Setting Up the Game Environment

Before we start coding, make sure you have Python 3 installed on your computer. You can write Python code in any text editor, but using an Integrated Development Environment (IDE) like PyCharm, Visual Studio Code, or IDLE (Python's built-in editor) can provide helpful features like code autocompletion and error checking.

Guess the number game in Python
Guess the number game in Python

For this tutorial, we'll use a simple text file for our code to keep it accessible and easy to understand. Save the following initial code in a file named `guessing_game.py`.

Including the Required Libraries

🎯 Build a Fun Python Project – Number Guessing Game (Free Code Inside!)
🎯 Build a Fun Python Project – Number Guessing Game (Free Code Inside!)

The `random` library in Python allows us to generate random numbers. To use this functionality, we need to import the `randint` function from the `random` module. Here's how you do it:

import random

Variables for the Game

Number Guessing Game in Python with Source Code
Number Guessing Game in Python with Source Code

Next, we'll set up some variables to store values like the secret number and the maximum number of attempts. Initialize these variables in your script:

secret_number = random.randint(1, 100)
max_attempts = 10
attempts = 0

Creating the Game Loop

Python - Number guessing game
Python - Number guessing game

Now, we'll create a loop structure that keeps the game running until the player guesses the correct number or exceeds the maximum number of attempts.

Python uses a `while` loop for iterating until a specific condition is met. In this case, the condition is whether the player has made their maximum number of attempts:

Python Project for Beginners| Build Guess the Number #pythonforbeginners #python     #pythonprojects
Python Project for Beginners| Build Guess the Number #pythonforbeginners #python #pythonprojects
Python Challenge 💯: Python Number Guessing 🎯 Game
Python Challenge 💯: Python Number Guessing 🎯 Game
Guess the Number Game -
Guess the Number Game -
Python Number Guessing Game (Beginner Project)
Python Number Guessing Game (Beginner Project)
Guess a Number Game In Python - Python Tutorials
Guess a Number Game In Python - Python Tutorials
Number Guessing Game in Python || Python Tutorial for Beginners ||  Eana Ali
Number Guessing Game in Python || Python Tutorial for Beginners || Eana Ali
Game Development with Python Programming...
Game Development with Python Programming...
Game Development with Python Programming...
Game Development with Python Programming...
How To Create Guess Game in Python - Python Mini Project Tutorials
How To Create Guess Game in Python - Python Mini Project Tutorials
Python & PyGame Tutorial – Code a Duck Hunt Game
Python & PyGame Tutorial – Code a Duck Hunt Game
Game Development with Python Programming...
Game Development with Python Programming...
Hangman Game in Python - Simple Game Project for Beginners - DataFlair
Hangman Game in Python - Simple Game Project for Beginners - DataFlair
Python Project: Guessing Game
Python Project: Guessing Game
Pinball Game development using Python
Pinball Game development using Python
Dice in python
Dice in python
🎯 PRIME NUMBER CHECKER using PYTHON🔖 Programming Large Numbers In Python, How To Generate Random Numbers In Python, How To Accept 3 Numbers In Python, Numbers Type In Python, Python Code Comparison Beginner Expert, Python Program For Number Addition, Python Prime Number Program, Qgis Processing Python, Python Program For Numerical Pyramid
🎯 PRIME NUMBER CHECKER using PYTHON🔖 Programming Large Numbers In Python, How To Generate Random Numbers In Python, How To Accept 3 Numbers In Python, Numbers Type In Python, Python Code Comparison Beginner Expert, Python Program For Number Addition, Python Prime Number Program, Qgis Processing Python, Python Program For Numerical Pyramid
Primeros pasos recreando Pokémon: Resoluciones y escalas en Pygame #python #programacion #javascript
Primeros pasos recreando Pokémon: Resoluciones y escalas en Pygame #python #programacion #javascript
How To Make A Pacman Game In Python
How To Make A Pacman Game In Python
Python Games for Kids to Code
Python Games for Kids to Code
Creating a Snake Game in Python.
Creating a Snake Game in Python.

while attempts < max_attempts:

Prompting the Player for Their Guess

Inside the loop, ask the player to enter their guess using the `input` function. Convert the player's input to an integer using the `int()` function, and increment the attempts counter:

guess = int(input("Guess the number (1-100): "))
attempts += 1

Providing Feedback on the Player's Guess

Now, let's give the player feedback based on their guess. Use an `if-elif-else` statement to compare the player's guess with the secret number and provide the appropriate message:

if guess == secret_number:
    print("Congratulations! You guessed the number.")
    break
elif guess < secret_number:
    print("Too low! Please try again.")
else:
    print("Too high! Please try again.")

Adding a Game Intro and Goodbye Message

Let's enhance our game by adding an introduction message that tells the player the game's objective and maximum number of attempts. We'll also display a goodbye message when the game ends. Add these changes outside the game loop:

print("Welcome to the Number Guessing Game!\nI'm thinking of a number between 1 and 100.\nYou have 10 attempts to guess the number.")

(Inside the loop)


if attempts == max_attempts:
    print("Sorry, you couldn't guess the number. The correct number was", secret_number)

With this, you've created an interactive number guessing game using Python. Now it's time to run your game, test it, and share it with your friends. Happy coding!