# 'Guess the Number' game with Python: Basic Project

## Learn how to create an easy 'Guess the Number' game using Python's random module and basic syntax, perfect for beginners.

### Table of contents

Hey readers, welcome to this week's article. Today, I am going to tell you about a famous coding mini-project known as "Guess the number". If you are new to Python programming then this project can help you flaunt your coding skills among your peers. Hope you are loving my articles and I appreciate it if you show a response by commenting, liking or even sharing on your socials. I am focusing more on Python so I also suggest you to read my previous articles on Python. Here's the link.

"Guess the Number" is a classic game that involves a player guessing a random number generated by the computer. The player has to guess the number within a certain number of attempts, and the computer provides hints to the player to help them guess the number. In this article, we will implement the "Guess the Number" game in Python.

## Making pieces!

The first step in implementing the game is to generate a random number. We can use the `random`

module in Python to generate a random number between a specified range. Here's how we can generate a random number between 1 and 100:

```
import random
secret_number = random.randint(1, 100)
```

Next, we need to ask the player to guess the number. We can use the `input()`

function to get input from the player. Since the player's guess is a number, we need to convert the input string to an integer using the `int()`

function.

```
user_guess = int(input("Guess a number between 1 and 100: "))
```

After the player has made a guess, we need to check if the guess is correct. If the guess is correct, we can print a message to the player and end the game. If the guess is incorrect, we can provide hints to the player to help them guess the number.

```
if user_guess == secret_number:
print("Congratulations! You guessed the number.")
break
else:
print("Sorry, that's not the number.")
if user_guess < secret_number:
print("The number is higher.")
else:
print("The number is lower.")
```

We can also keep track of the number of attempts made by the player and end the game if the player has exceeded the maximum number of attempts.

```
attempts = 0
max_attempts = 10
while attempts < max_attempts:
user_guess = int(input("Guess a number between 1 and 100: "))
attempts += 1
if user_guess == secret_number:
print("Congratulations! You guessed the number in", attempts, "attempts.")
break
else:
print("Sorry, that's not the number.")
if user_guess < secret_number:
print("The number is higher.")
else:
print("The number is lower.")
else:
print("Sorry, you have exceeded the maximum number of attempts.")
```

## Final Code - Joining the pieces

Putting it all together, here's the complete code for the "Guess the Number" game in Python:

```
import random
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 10
while attempts < max_attempts:
user_guess = int(input("Guess a number between 1 and 100: "))
attempts += 1
if user_guess == secret_number:
print("Congratulations! You guessed the number in", attempts, "attempts.")
break
else:
print("Sorry, that's not the number.")
if user_guess < secret_number:
print("The number is higher.")
else:
print("The number is lower.")
else:
print("Sorry, you have exceeded the maximum number of attempts.")
```

In conclusion, the "Guess the Number" game is a fun and simple game that can be easily implemented in Python. By using the `random`

module and basic Python syntax, we can create a game that provides an enjoyable experience for the player.

So, this was all from this week. Please don't forget to like, comment and share and you can also check out my other articles. Also, don't forget to subscribe to the newsletter (CLICK HERE) which will keep you updated about new uploads.

Thank you for reading. Signing off!