How to Create a \'Guess the Number\' Game in Python for beginners

WBOY
Release: 2024-08-12 18:39:39
Original
377 people have browsed it

How to Create a

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
Copy after login

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)
Copy after login

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.")
Copy after login

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
Copy after login

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!")
Copy after login

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!")
Copy after login

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!")
Copy after login

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!!

Wanna be the master of python click here.

The above is the detailed content of How to Create a \'Guess the Number\' Game in Python for beginners. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template