Number Guessing Game using Python

Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 1277


Number Guessing Game using Python

We must construct a program to choose a random number between 1 and 10 in order to create a guessing game. We can use conditional statements to tell the user if the guessed number is smaller, larger than, or equal to the randomly chosen number in order to provide tips to the user.

So the following describes how to develop a Python program to make a game of number guessing:


#ScriptsKart

import random # Importing the 'random' module to generate random numbers

# Generating a random number between 1 and 9
n = random.randrange(1, 10)

# Asking the user to input a number
guess = int(input("Enter any number: "))

# A loop to keep asking for input until the guessed number matches the generated number
while n != guess:
# Checking if the guessed number is lower than the generated number
if guess < n:
print("Too low")
guess = int(input("Enter number again: "))
# Checking if the guessed number is higher than the generated number
elif guess > n:
print("Too high!")
guess = int(input("Enter number again: "))
else:
# If the guessed number matches the generated number, exit the loop
break

# Printing a success message when the guessed number matches the generated number
print("You guessed it right!!")

#ScriptsKart