Creating a Simple Game in Python: Guess the Number

Games are a fun way to learn programming, and Python is a great language for beginners to start with. In this blog post, we’ll explore how to create a simple “Guess the Number” game using Python. The game will randomly generate a number between 1 and 100, and the player will have to guess the number in a limited number of attempts.

Game Setup

Let’s start by importing the necessary modules and initializing some variables. We’ll use the random module to generate a random number and the input function to get the player’s guesses.

pythonimport random

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

# Initialize variables to track guesses and attempts
guess = None
attempts = 0

Game Loop

Now, let’s create a game loop that will repeatedly prompt the player for guesses until they guess correctly or exceed the maximum number of attempts.

pythonwhile guess != secret_number and attempts < 10:
guess = int(input("Guess a number between 1 and 100: "))
attempts += 1

if guess < secret_number:
print("Too low. Try again.")
elif guess > secret_number:
print("Too high. Try again.")

Ending the Game

After the game loop, we’ll check if the player guessed correctly or exceeded the maximum number of attempts.

pythonif guess == secret_number:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
else:
print(f"Sorry, you exceeded the maximum number of attempts. The number was {secret_number}.")

Complete Code

Here’s the complete code for the “Guess the Number” game:

pythonimport random

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

# Initialize variables to track guesses and attempts
guess = None
attempts = 0

while guess != secret_number and attempts < 10:
guess = int(input("Guess a number between 1 and 100: "))
attempts += 1

if guess < secret_number:
print("Too low. Try again.")
elif guess > secret_number:
print("Too high. Try again.")

if guess == secret_number:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
else:
print(f"Sorry, you exceeded the maximum number of attempts. The number was {secret_number}.")

Playing the Game

To play the game, simply run the code in a Python environment, such as a code editor or an interactive Python shell. The game will prompt you to guess a number between 1 and 100, and it will give you feedback on whether your guess is too high or too low. If you guess correctly within 10 attempts, you’ll win the game. Otherwise, you’ll be told the correct number.

Conclusion

Creating a simple game like “Guess the Number” in Python is a great way to learn the basics of programming and logic. By extending this game, you can add more features, such as keeping track of the player’s high score or allowing them to choose the difficulty level. Have fun exploring the world of game development with Python!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *