Signup/Sign In

Number Guessing Game In Python

Number Guessing Game In Python

A number guessing game is a frequent mini-project for beginner programmers who grip random number generation and conditional statements with iteration.

The number guessing game is built on the player's notion to estimate a number between the provided range. If the player predicts the desired number, the player wins; otherwise, the player loses the game. Since this game has limited efforts thus, the player needs to indicate the number with the limited attempts. Otherwise, the player will lose the game.

In this tutorial, we will construct a number guessing game in Python.

Number Guessing Game Rules

  1. You must input only valid integers within the provided range.
  2. You will be granted limited tries to estimate the number.
  3. You cannot exit the game once begun.

If the input number is less than or more significant than the needed number, the player receives the message (hint) to move further in up or down range.

We first produce a random number between a defined range in such a game. We ask the user to estimate this number. If the guess is accurate, we report that the guess is correct and break out of the loop. Else we specify whether the number is less or more significant than the actual number. We also ask the user for the total guesses they are authorized to take. When the number of guesses surpasses this, we break off the loop.

The user may make use of this to know the actual number. For example, if the user guesses that the number is 45 and the result is that the exact number is smaller than 45, the user might infer that the number won’t lie between 45 and 100 (provided that the range is up to 100). (given that the content is till 100). This way, the user may keep guessing and interpreting the outcome. We publish the number of guesses it takes the user to get the answer correctly.

Number Guessing Game Implementation in Python Language

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 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: "))
            print("The number guessed is right")
            print("Total guesses taken: ", t+1)
        print("Ran out of tries!")


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 generated the program in Python 3.
Here are the instructions for making a number guessing game in Python:

  1. We initially asked the user to choose the range for the number to be created. A random integer is created using the randint() function from the unexpected package.
  2. We started a variable with 0 to keep track of the total predictions made.
  3. We executed the while loop until the number estimated was not equal to the actual number.
  4. We used an if-else ladder to check whether the estimated number is lower or greater than the actual number and increase the total guesses in each iteration.
  5. We broke out of the loop when the estimate matched the number.
  6. We displayed the total guesses made when the guess was accurate.

Similarly replicating the logic, we may make this game in Python 2 or any other computer language.

About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.