How to build a Guess the Number Game in Python
Tags: python, codenewbie, tutorial, beginners
Published: 2021-02-20 18:05:17

Hello everyone, today we are going to build a **Guess the Number Game in Python**!

## How does it work?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gg111l27gu53mk928ukl.png)

Our games will randomly generate a number between **0 and 30** and the player has to guess the number.

If the number entered by the player is less than generated number then the player will be prompted with **`too low`** message!

And if the number entered by the player is more than generated number then the player will be prompted with **`too high`** message!

This process will be repeated until the player finds the right number. 

Pretty straightforward right... let's do it!



## Let's Code

Now since we are going to generate a random number and if you've been following past tutorials in the series than you might know what we are talking about here. We are going to import a module that comes pre-installed with our Python, it's called **`random`.** 

Let's import it into our project.

```python
import random
```

Now we have to initialize a **`max_num`** variable. So that we can customize the difficulty as per our choice. Higher the value of **`max_num`**, the higher the difficulty. For now, let's keep it 30.

```python
max_num = 30
```

Now it's time we generate our random number which the player has to find. 

For that we will be using **`randint()`** function from the **`random`** module. We will store this random value in **`random_number`** variable.

```python
random_number = random.randint(1, max_num)
```

It will generate a random number between **`1`** & **`max_num`.**

Then we will also initialize a **`guess`** variable to store the player's answer for comparison.

```python
guess = 0
```

Now let's create a while loop to keep asking the player until the right number is found. We will keep this loop running until our **`random_number`** & guess matches.

```python
while guess != random_number:
	pass
```

Alright, so it's time we ask our player to make a guess enter a number to start the game.

```python
while guess != random_number:
	guess = int(input(f"Guess the number between 1 & {max_num}: "))
```

We will also use **`int()`** function to covert the number from a string into an integer so that we can compare it to check the answer.

Now we will make use of **`if`** conditionals to compare the answer and provide suitable feedback to the player.

```python
while guess != random_number:
	guess = int(input(f"Guess the number between 1 & {max_num}: "))
	if guess < random_number:
	        print("Wrong! Too low...")
	elif guess > random_number:
	        print("Wrong! Too high...")
```

Hopefully, the logic of the while loop will be clear as we already discussed it in **How does it work?** section.

Here are our **`while`** loop ends.

Now one last thing we have to do is to print a final message to the screen if the player got the right answer.

```python
print(f"Thats Right! Random number is {random_number}")
```

**Let's summarize the logic one again:**

- **`max_num`** variable will decide the difficulty of our game. Higher the value, the higher the difficulty.
- We will use **`random.randint(1, max_num)`** function to generate a random number.
- **`guess`** variable will contain the answer entered by the player.
- Now loop will begin and if the number entered by the player matches the generated answer then the loop will no longer execute and the final **`print()`** statement will be printed, telling the player that the game is over.
- Otherwise, the loop will keep running until the right number is entered by the player.

## Source Code

You can find the complete source code of this project here -

[mindninjaX/Python-Projects-for-Beginners](https://github.com/mindninjaX/Python-Projects-for-Beginners/tree/master/Guess%20the%20number%20(computer))

## Support

Thank you so much for reading! I hope you found this beginner project useful. 

If you like my work please consider [Buying me a Coffee](https://buymeacoff.ee/mindninjaX) so that I can bring more projects, more articles for you.

![https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png](https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png)

Also if you have any questions or doubts feel free to contact me on [Twitter](https://twitter.com/mindninjaX), [LinkedIn](https://www.linkedin.com/in/mindninjax/) & [GitHub](https://github.com/mindninjaX). Or you can also post a comment/discussion & I will try my best to help you :D