Building a number guessing game is a fun and easy way to learn basic programming concepts like loops, conditionals, and user input handling. In this guide, we’ll walk you through creating a simple number guessing game in Python. This game will generate a random number, and the player will have to guess it. The game will provide hints whether the guess is too high or too low, and the player will continue guessing until they find the correct number.
Step 1: Setting Up Your Python Environment
Before we begin, make sure that you have Python installed on your system. If you don’t have it installed, you can download it from the official Python website. You can also use an integrated development environment (IDE) like PyCharm, Visual Studio Code, or even a simple text editor to write your Python code.
Step 2: Import Required Libraries
In Python, we will use the random
module to generate a random number for the game. The input()
function will be used to get the user’s guess. There’s no need for external libraries, just the standard Python library.
import random
Step 3: Generate a Random Number

Next, we will create a random number within a specified range. For simplicity, let’s make the number between 1 and 100. You can adjust the range based on your preference.
random_number = random.randint(1, 100)
The random.randint(1, 100)
function generates a random number between 1 and 100 (inclusive).
Step 4: Introduction and User Instructions
Now, we will display an introductory message that explains how the game works.
print("Welcome to the Number Guessing Game!")
print("I am thinking of a number between 1 and 100.")
print("Can you guess what it is?")
Step 5: Take User Input
Next, we’ll ask the user for their guess. The input()
function is used to capture input from the user. We need to convert the input to an integer since input()
returns a string by default.
user_guess = int(input("Enter your guess: "))
Step 6: Check the User’s Guess
Now, we need to check if the user’s guess is correct, too high, or too low. We will use an if-elif-else
statement to handle the different conditions.
if user_guess < random_number:
print("Too low! Try again.")
elif user_guess > random_number:
print("Too high! Try again.")
else:
print("Congratulations! You guessed the correct number.")
Here’s what each condition does:
- If the guess is too low, it will print “Too low! Try again.”
- If the guess is too high, it will print “Too high! Try again.”
- If the guess is correct, it will print “Congratulations! You guessed the correct number.”
Step 7: Allow Multiple Attempts
At this point, the game will end after the first guess. To make the game more interactive, let’s add a loop to allow the user to keep guessing until they find the correct number.
We will use a while
loop, which will keep running as long as the user’s guess is not correct.
while user_guess != random_number:
user_guess = int(input("Enter your guess: "))
if user_guess < random_number:
print("Too low! Try again.")
elif user_guess > random_number:
print("Too high! Try again.")
else:
print("Congratulations! You guessed the correct number.")
This loop will continue until the user guesses the correct number. If the guess is too high or too low, the loop will prompt the user for another guess.
Step 8: Add a Counter for the Number of Attempts
To make the game even more engaging, let’s add a counter that will track how many guesses the player has made. This will allow the player to know how many attempts it took to guess the correct number.
We can create a variable attempts
and increment it each time the user makes a guess.
attempts = 0
while user_guess != random_number:
user_guess = int(input("Enter your guess: "))
attempts += 1
if user_guess < random_number:
print("Too low! Try again.")
elif user_guess > random_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the correct number in {attempts} attempts.")
Now, the player will see how many attempts it took to guess the correct number once they win the game.
Step 9: Play Again Option
It would be fun to allow players to play multiple rounds of the game without restarting the program. We can add a prompt asking the player if they want to play again after they win a round.
Here’s how to do it:
while True:
random_number = random.randint(1, 100)
user_guess = int(input("Enter your guess: "))
attempts = 0
while user_guess != random_number:
user_guess = int(input("Enter your guess: "))
attempts += 1
if user_guess < random_number:
print("Too low! Try again.")
elif user_guess > random_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the correct number in {attempts} attempts.")
break
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != "yes":
print("Thanks for playing! Goodbye.")
break
This code will ask the user after each round if they want to play again. If they type “yes,” the game will start over with a new random number. If they type “no,” the game will end.
Step 10: Final Code
Here’s the final version of the number guessing game that includes all the features:
import random
def number_guessing_game():
while True:
print("Welcome to the Number Guessing Game!")
print("I am thinking of a number between 1 and 100.")
random_number = random.randint(1, 100)
user_guess = int(input("Enter your guess: "))
attempts = 0
while user_guess != random_number:
user_guess = int(input("Enter your guess: "))
attempts += 1
if user_guess < random_number:
print("Too low! Try again.")
elif user_guess > random_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the correct number in {attempts} attempts.")
break
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != "yes":
print("Thanks for playing! Goodbye.")
break
# Start the game
number_guessing_game()
Conclusion
Congratulations! You have now built a simple number guessing game in Python. This game uses basic programming concepts like loops, conditionals, user input, and random number generation. You can always enhance the game by adding more features, such as giving the player a limited number of guesses, offering hints based on previous guesses, or adding a scoring system.
Building games like this one is a great way to practice programming and improve problem-solving skills. Happy coding!
Read more these type of posts regarding coding please visit : Digital News Reporting