So Today we will be creating an awesome **Guess The Number ** Python Game. This is a python project for beginners and will use the basics of python.

## Game Rules :

- Computer will generate a random Number.
- Player will have to guess the number in given attempts.
- For every attempt, computer will give hint either to choose higher or lower number
- If player guesses the number in given attempts , he will win and be given score accordingly.

*Let’s Start*

*you are reading python game : guess the number game in python*

### Generating a Random Number

To generate a Random Number between 1 to 100 , We will be using a module named ‘**random’ . **

```
import random as r
random_number = r.randrange(1,100)
print(random_number)
```

### Guessing the Number

Now we will ask user to guess the number , simply by **user input.**

`guess_number = input('Guess the Number : ')`

### Checking the Number

Now we will compare **Guess_number **with **Random_number **by using if-elif statement.

```
if(random_number == guess_number):
print('You Won')
elif(random_number > guess_number):
print('Hint : Choose a Higher Number')
else:
print('Hint : Choose a Lower Number')
```

### Adding Loop so that Game Doesn’t end in First Guess

```
while(True):
guess_number = int(input('Guess the Number : '))
if(random_number == guess_number):
print('You Won')
elif(random_number > guess_number):
print('Hint : Choose a Higher Number')
else:
print('Hint : Choose a Lower Number')
```

### Game Doesn’t end if i win ?

Now game is working fine , though even after I have guessed the number , it keeps asking for more . It’s because I have used infinite loop . All I need to do is when I have guessed the number , I will use **break **to get out of the loop.

*you are reading python game : guess the number game in python*

```
while(True):
guess_number = int(input('Guess the Number : '))
if(random_number == guess_number):
print('You Won')
```**break**
elif(random_number > guess_number):
print('Hint : Choose a Higher Number')
else:
print('Hint : Choose a Lower Number')

### Adding Maximum Attempts in Game

Let’s add that user must guess in 10 attempts and if he is not able to guess in 10 attempts , he will lose as he ran out of his chances. For this we will use **else **of **while **loop.

*you are reading python game : guess the number game in python*

```
chances = 1
while(chances<=10):
print(f'YOU HAVE {10-chances} LEFT')
guess_number = int(input('Guess the Number : '))
if(random_number == guess_number):
print('You Won')
break
elif(random_number > guess_number):
print('Hint : Choose a Higher Number')
else:
print('Hint : Choose a Lower Number')
chances = chances + 1
else:
print('Oops!! You lost , ran out of your chances.')
```

### Adding Score

Now adding score is not a big deal, all you need to do is to create a formulae that scores big when user has guessed in less number of attempts.

*you are reading python game : guess the number game in python*

```
score = 110
chances = 1
while(chances<=10):
print(f'YOU HAVE {10-chances} CHANCES LEFT')
guess_number = int(input('Guess the Number : '))
if(random_number == guess_number):
print(f'You Won. Score : {score-10*chances} ')
break
elif(random_number > guess_number):
print('Hint : Choose a Higher Number')
else:
print('Hint : Choose a Lower Number')
chances = chances + 1
else:
print('Oops!! You lost , ran out of your chances.')
```

## Whole Code

```
import random as r
random_number = r.randrange(1,100) #Generating Random Number
print(random_number)
score = 110
chances = 1
while(chances<=10):
print(f'YOU HAVE {10-chances} CHANCES LEFT')
guess_number = int(input('Guess the Number : '))
if(random_number == guess_number):
print(f'You Won. Score : {score-10*chances} ')
break
elif(random_number > guess_number):
print('Hint : Choose a Higher Number')
else:
print('Hint : Choose a Lower Number')
chances = chances + 1
else:
print('Oops!! You lost , ran out of your chances.')
```

## Conclusion

This **Python Project **used the basics of python like **conditional statements **and **loops.** You can make many changes in this game and make it more interesting. For anyone who is learning python , this little project will help him a lot to understand the basics of the python. To learn more about python , you can surf through our ConsoleFlare . To learn Python and become master in it, You can opt to our Course / Live Sessions . Check out our Courses here.

Show your love on Instagram : ConsoleFlare

*you were reading python game : guess the number game in python*

## 4 thoughts on “Python Game : How To Create A Numb3r Guessing Game In Python”

Comments are closed.

great submit, very informative. I ponder why the opposite specialists of this sector don’t notice this. You must continue your writing. I am confident, you’ve a huge readers’ base already!

I’m impressed, I need to say. Really not often do I encounter a blog that’s both educative and entertaining, and let me inform you, you’ve hit the nail on the head. Your concept is excellent; the difficulty is something that not enough people are talking intelligently about. I am very happy that I stumbled throughout this in my seek for one thing relating to this.

This is really interesting, You are a very skilled blogger. I’ve joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your site in my social networks!

Thanks for writing this great content without doubts is very informative