A number guessing game is a common mini-project for basic programmers who have a grasp on random number generation and conditional statements with iteration.

The number guessing game is based on a concept where player has to guess a number between given range. If player guesses the expected number then player wins else player loose the game. Since this game has limited attempts, so, player has to guess the number with the limited attempts, else player will lose the game.

In this article, we will create number guessing game in Python.

Table of Contents

## Number guessing game Rules

- You must enter only valid integer within the specified range.
- You will be provided limited attempts to guess the number.
- You cannot leave the game, once started.

If the entered number is less than or greater than the required number, then player gets the message (hint) to proceed further either in up or down range.

In such a game, we first generate a random number between a given range. We ask the user to guess this number. If the guess is right, we print that the guess is right and break out of the loop. Else we tell whether the number is less or more than the actual number. We also ask the user for the total guesses he or she is allowed to take. When the number of guesses exceeds this, we break off the loop.

The user can take help of this to know the actual number. For example, if the user guesses that the number is 45 and the output is that the actual number is less than 45, then the user can interpret that the number won’t lie between 45 and 100 (given that the range is till 100). This way the user can keep guessing and interpreting the result. We print the number of guesses it takes the user to get the answer right.

## Number guessing game implementation in Python

Here is implementation of number guessing game in Python.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import random t = 0 g = int(input("Total Guesses: ")) low = int(input("Enter the lower range: ")) high = int(input("Enter the upper range: ")) x = random.randint(low, high) n = int(input("Enter an integer between the given range: ")) while (x != 'n'): if(t<(g-1)): if n < x: print("The number guessed is low") t = t+1 n = int(input("Enter an integer between the given range: ")) elif (n > x): print("The number guessed is high") t = t+1 n = int(input("Enter an integer between the given range: ")) else: print("The number guessed is right") print("Total guesses taken: ", t+1) break else: print("Ran out of tries!") break |

Output:

Enter the lower range: 0

Enter the upper range: 7

Enter an integer between the given range: 5

The number guessed is low

Enter an integer between the given range: 6

The number guessed is right

Total guesses taken: 2

We created the above program in Python 3.

Here are the steps for creating number guessing game in Python:

- We first asked the user to specify the range for the number to be generated. A random number is generated using the
`randint()`

function from the random module. - We initialized a variable with 0 to keep track of the total guesses made.
- We ran the while loop till the number guessed is not equal to the actual number.
- We used an
`if-else`

ladder to check if the guessed number is smaller or bigger than the actual number and increment the total guesses in each pass. - We broke out of the loop when the guess matches the number.
- We printed the total guesses taken when the guess is right.

By similarly implementing the logic, we can create this game in Python 2 or some other programming language.

That’s all about Number guessing game in Python.