Divyansh Tiwari

Posted on

# How to Create a "Guess the Number" Game in Python for beginners

## What Is the "Guess the Number" Game?

In this game, the computer randomly picks a number, and you have to guess what it is. After each guess, the computer will tell you if your guess is too high, too low, or just right. The game ends when you guess the correct number, and it also tells you how many attempts it took.

Let's dive right in!

Step 1: Import the random Module
First, we need to import the random module. This module helps us generate a random number that you will try to guess.

``````import random
``````

Step 2: Generate a Random Number
Now, we need to generate a random number between 1 and 100. This number will be the secret number that you have to guess.

``````# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
``````

Step 3: Start the Game and Explain the Rules
Next, let's display a welcome message to the player and explain the rules.

``````# Start the game
print("Welcome to 'Guess the Number' game!")
print("I'm thinking of a number between 1 and 100.")
``````

Step 4: Create a Loop for Guessing
We'll create a loop that keeps asking the player to guess the number until they get it right. We'll also keep track of how many guesses the player makes.

``````# Variable to store the user's guess
guess = None

# Variable to count the number of attempts
attempts = 0
``````

Step 5: Ask for the Player's Guess
In this step, we'll ask the player to enter their guess. After they guess, we'll check if the guess is too high, too low, or correct.

``````# Loop until the user guesses the correct number
while guess != secret_number:
# Ask the user to enter a number
guess = int(input("Enter your guess: "))

# Increment the attempts counter
attempts += 1

# Check if the guess is too low, too high, or correct
if guess < secret_number:
print("Too low! Try guessing a higher number.")
elif guess > secret_number:
print("Too high! Try guessing a lower number.")
else:
print("Congratulations! You guessed the correct number!")
``````

Step 6: Display the Number of Attempts

Finally, after the player guesses the number, we'll let them know how many attempts it took to find the correct answer.

``````# Tell the user how many attempts it took
print(f"It took you {attempts} attempts to guess the correct number.")
print("Thank you for playing!")
``````

Complete Code
Here's the full code for the game:

``````import random

# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)

# Start the game
print("Welcome to 'Guess the Number' game!")
print("I'm thinking of a number between 1 and 100.")

# Variable to store the user's guess
guess = None

# Variable to count the number of attempts
attempts = 0

# Loop until the user guesses the correct number
while guess != secret_number:
# Ask the user to enter a number
guess = int(input("Enter your guess: "))

# Increment the attempts counter
attempts += 1

# Check if the guess is too low, too high, or correct
if guess < secret_number:
print("Too low! Try guessing a higher number.")
elif guess > secret_number:
print("Too high! Try guessing a lower number.")
else:
print("Congratulations! You guessed the correct number!")

# Tell the user how many attempts it took
print(f"It took you {attempts} attempts to guess the correct number.")
print("Thank you for playing!")
``````

And that's it! You've just created a simple "Guess the Number" game in Python. This project is perfect for beginners and helps you understand the basics of loops, conditionals, and user input in Python. Keep practicing, and soon you'll be ready to create more complex projects!

Happy coding!!