Guessing Game with Python

# 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!!  
Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1537

2 Comments

Leave a Reply