Guessing Game with Python

Python Tutorial to create a number guessing game
# to import random module
import random
# to select a random number between 1 to 10
n = random.randrange(1,10)
# to take a user input to enter a number
guess = int(input("Enter any number: "))
while n!= guess: # means if n is not equal to the input guess
    # if guess is smaller than n
   if guess < n:
      print("number is more than",guess)
      # to again ask for input
      guess = int(input("Enter number again: "))
  # if guess is greater than n
   elif guess > n:
      print("number is less than",guess)
      # to again ask for the user input
      guess = int(input("Enter number again: "))
  # if guess gets equals to n terminate the while loop
print("you guessed it right!!")
Enter any number: 3
number is more than 3
Enter number again: 6
number is more than 6
Enter number again: 8
number is more than 8
Enter number again: 9
you guessed it right!!  
Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.


  1. thanks for this, I was finding it a lot

Leave a Reply

Data Science | Machine Learning | Python | C++ | Coding | Programming | JavaScript