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
   else:
       break
print("you guessed it right!!")
#Output
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!!  

2 Comments

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

Leave a Reply