Building a Number Guessing Game in Python

Hands-on Mentor
4 min readMar 21, 2023

--

In this tutorial, we will be building a number-guessing game using conditional statements, loops, and functions in Python. This game will require the user to guess a randomly generated number within a specific range. Let’s get started!

Step 1: Import necessary modules

First, we need to import the random module which will be used to generate a random number.

import random

Step 2: Define the main function

We will define a function called guessing_game which will contain the main logic of the game. This function will take two parameters start and end which will define the range of numbers that can be guessed.

def guessing_game(start, end):
pass

Step 3: Generate the random number

Inside the guessing_game function, we will generate a random number using the randint function from the random module. The randint the function takes two parameters that define the range of numbers that can be generated.

def guessing_game(start, end):
number = random.randint(start, end)

Step 4: Implement the game logic

Now we will implement the main logic of the game. We will use a while loop to prompt the user to input a number until they guess the correct number. Inside the loop, we will use conditional statements to check if the user's guess is too high or too low.

def guessing_game(start, end):
number = random.randint(start, end)
guess = None
while guess != number:
guess = int(input(f"Guess a number between {start} and {end}: "))
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
print("Congratulations! You guessed the number!")

In this part of the code, the game logic is implemented. A while loop is used to continually prompt the user for their guess until they correctly guess the randomly generated number. Inside the loop, an if statement is used to check if the user's guess is too high or too low. If the guess is too low, the message "Too low!" is printed to the console. If the guess is too high, the message "Too high!" is printed to the console. The loop will continue until the user's guess is equal to the randomly generated number. Once the correct number is guessed, the message "Congratulations! You guessed the number!" is printed to the console and the loop exits.

Step 5: Test the game

Now we can test the game by calling the guessing_game function and passing in the range of numbers that can be guessed.

guessing_game(1, 100)

This will generate a random number between 1 and 100 and prompt the user to guess the number until they guess correctly.

In this number guessing game, the difficulty level is determined by the range of numbers that can be guessed. The wider the range, the harder the game. In the provided code, the start and end parameters of the guessing_game function determine the range of numbers that can be guessed. To increase the difficulty level, you can increase the range of numbers. For example, changing guessing_game(1, 100) to guessing_game(1, 1000) would make the game harder as the range of numbers to be guessed is larger.

Step 6: Implement the maximum try number

To add a maximum number of tries, we can add a counter variable to keep track of the number of tries and add an if statement inside the while loop. If the user exceeds the maximum number of tries, the loop will exit and the game will end.

def guessing_game(start, end, max_tries):
number = random.randint(start, end)
guess = None
tries = 0
while guess != number and tries < max_tries:
guess = int(input(f"Guess a number between {start} and {end}: "))
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
tries += 1
if guess == number:
print("Congratulations! You guessed the number!")
else:
print(f"Sorry, you ran out of tries. The number was {number}.")

In this updated code, we added a new parameter to the guessing_game function, max_tries, which determines the maximum number of tries allowed. We also added a tries variable to keep track of the number of tries.

Inside the while loop, we added an if statement to check if the number of tries has exceeded the maximum number of tries. If the user has exceeded the maximum number of tries, the loop will exit and the game will end. If the user guesses the correct number before exceeding the maximum number of tries, the loop will exit and the message "Congratulations! You guessed the number!" will be printed to the console. If the user exceeds the maximum number of tries without guessing the correct number, the message "Sorry, you ran out of tries. The number was [number]." will be printed to the console.

Step 7: Test the game

Now we can test the updated game by calling the guessing_game function and passing in the range of numbers that can be guessed and the maximum number of tries allowed.

guessing_game(1, 100, 5)

This will generate a random number between 1 and 100 and prompt the user to guess the number until they guess correctly or run out of tries.

Conclusion

In this tutorial, we learned how to build a number guessing game in Python using conditional statements, loops, and functions. We added a new feature to the game by implementing a maximum number of tries allowed. This game can be further customized by adding additional features such as a score system or implementing different difficulty levels.

--

--