# Guess The Number Game in Python - Best Mini Project

**Guess the number game in python** is the best mini project for beginners**.** So let’s develop guess the number game in python.

## What we are gonna do?

The random function generates the number between 1 to 100. The user has 10 chances to guess the number. So, we ask max 10 times to user to enter a number. Once the user guess is matched with an actual number then the game is over. Every time user also sees how many no. of chances are left. If the user can’t guess the number then the game is over. Simple!!

## Which Python concepts are covered?

- If-else
- While Loop
- Random function
- break statement

## Let’s code

**Imports and declaration**

```
import random
n = random.randint(1, 100)
count = 1
guess_chances = 10
```

So we need a random module to generate a random number. Variable `n`

holds the one random number between 1 to 100. `count`

variable for the count the number of guess user took, initially 1. `guess_chances`

holds a number of chances code give to a user to guess the number.

**Inside the while loop**

```
num = int(eval(input("Guess the Number: ")))
if num > n:
print("Your guess was too high: Guess a number lower than", num)
elif num < n:
print("Your guess was too low: Guess a number higher than", num)
else:
print("You Win!")
print(count, "gueses you took")
break
```

We ask the user to guess the number. And store guessed number in `num`

variable. Then we do comparisons with the generated number `n`

.

In the first condition, if the guessed number is higher then the actual number than prints **Your guess was too high: Guess a number lower than** (your number). So, the user can get the idea, and next time he/she enters a lower number.

If the first condition is false, then flow goes for check second condition, if the guessed number is lower than actual number then prints **Your guess was too low: Guess a number higher than** (your number). So, the user can get the idea, and next time he/she enters a higher number.

If above both condition returns false. Then the user guessed number is matched with the actual number and the loop will be terminated by a break statement. Code also prints **You Win!** message and number of guesses taken by the user that counted by count variable.

With this above all condition, code also prints the number of guesses left for the user, and the count variable increases by one in every iteration to print a number of guesses taken by the user once the user wins the game.

```
guess_chances -= 1
print(guess_chances, "Guesses Left")
count += 1
print()
```

So, the above all conditions are written in a while loop. **while** loop gets terminated once the user wins the game or no. of guesses reaches the limit.

```
while 1 <= guess_chances:
#above codes here
```

Outside of the loop, code will print the **Game Over** message with the actual number.

```
print("Game over")
print("Number is ", n)
```

## The full code

```
import random
n = random.randint(1, 100)
count = 1
guess_chances = 10
while 1 <= guess_chances:
num = int(eval(input("Guess the Number: ")))
if num > n:
print("Your guess was too high: Guess a number lower than", num)
elif num < n:
print("Your guess was too low: Guess a number higher than", num)
else:
print("You Win!")
print(count, "gueses you took")
break
guess_chances -= 1
print(guess_chances, "Guesses Left")
count += 1
print()
print("Game over")
print("Number is ", n)
# follow @code_snail on Instagram
```

## Output

```
Guess the Number: 66
Your guess was too high: Guess a number lower than 66
9 Guesses Left
Guess the Number: 44
Your guess was too low: Guess a number higher than 44
8 Guesses Left
Guess the Number: 55
Your guess was too high: Guess a number lower than 55
7 Guesses Left
Guess the Number: 50
Your guess was too low: Guess a number higher than 50
6 Guesses Left
Guess the Number: 53
Your guess was too high: Guess a number lower than 53
5 Guesses Left
Guess the Number: 52
Your guess was too high: Guess a number lower than 52
4 Guesses Left
Guess the Number: 51
You Win!
7 gueses you took
Game over
Number is 51
```

Hope you like this game. Now it’s your turn to *make guess the number in python* with some modifications. Share with your friends and family who learning python.

See other python code,