# Number guessing game in Python

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.

## Number guessing game Rules

1. You must enter only valid integer within the specified range.
2. You will be provided limited attempts to guess the number.
3. 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.

Output:

Total Guesses: 5
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.

## Related Posts

• 19 June

### Calculator program in Python

Table of ContentsUsing the while loop along with the if...else conditional statement.Define functions for Addition, Subtraction, Multiplication and DivisionTake user input using input functionComplete calculator program in Python A simple Calculator can be utilized to carry out the four basic arithmetic operations namely addition, division, multiplication, and subtraction depending on the input of the user. […]

• 31 May

### Perfect number program in Python

Table of ContentsUse the Simple iteration method to check whether a given number is a perfect number.Use the square root method to check whether a given number is a perfect number. According to number theory, a limb of pure mathematics that deals with integers, a Perfect Number can be defined as a positive integer whose […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.