 # Number Guessing Game Using Python

In this tutorial I’ll show you how to make a simple number guessing game using python.

Again we will use random module to generate random number.

To make this game we have to first generate a random number and we take input from the user and validate it. If the user input is the same as a random number then we simply print “You guessed it!!”, if the user input is greater than the random number then we print “guess is low!!” and if the user input number is less than the random number than we simply print “guess is low” on the console.

Make sure these conditional statements are wrapped inside while loop so it will continuously run until the user guess is right.

Here is complete number guessing game code using python.

``````import random
n = random.randint(1, 99)
guess = int(input("Enter a number from 1 to 99: "))
while n != "guess":
if guess < n:
print("guess is low")
guess = int(input("Enter a number from 1 to 99: "))
elif guess > n:
print("guess is high")
guess = int(input("Enter a number from 1 to 99: "))
else:
print("you guessed it!")
break``````

Here is the output of game.

In addition, we can also add a limit for user input. in other words, we can say we only give 10 chances to user for guessing the correct number.

So, how can we do that?

Again It’s very simple. We only run over loop for 10 times.

``````import random
n = random.randint(1, 99)
guess = int(input("Enter a number from 1 to 99: "))
max_chances = 0
while max_chances < 9:
max_chances += 1
if guess < n:
print("guess is low")
guess = int(input("Enter a number from 1 to 99: "))
elif guess > n:
print("guess is high")
guess = int(input("Enter a number from 1 to 99: "))
else:
print("you guessed it!")
print("You Won!!")
break

if guess != n:
print("Game Over")
print("Correct Number is:", n)``````

Related post: Simple Dice Game Using Python

##### You May Also Like ## Simple Dice Game Using Python

Spread the loveIn this tutorial we will make a simple dice game using python. So, let’s get started. To make this game we need one module: random We need to… ## Rock Paper Scissors Game Using Python

Spread the loveIn this tutorial I’ll show you how to make a simple rock paper scissors game using python. Here we need one module: random We need to use a… ## How to Get Wikipedia Results Using Python (GUI) 