PYTHON PROGRAM THAT GENERATES A RANDOM NUMBER BETWEEN 1 AND 10 AND ASKS THE USER TO GUESS THE NUMBER
Generate a random number between 1 and 10. Ask the user to guess the number and print a message based on whether they get it right or not.
CODE:
import random
number=random.randint(1,10)
guess_number=0;
count=0;
while guess_number!=number:
guess_number=int(input("Guess the number:"))
count+=1
if guess_number<number:
print("You have guessed a smaller number")
elif guess_number>number:
print("You have guessed a bigger number")
else:
print("Your guess is right")
print("You have tried to guess the random number in",count,"tries")
print("Done")
EXPLANATION:
In this program, firstly we import random module (in-built module in python) into the program .
Then to generate a random number between 1 and 10, we use randint() function present in random module.
The program will ask the user to guess the number.It will then print a message based on whether they get it right or wrong.
The program will also count number of chances taken by the user to guess the number by initializing count variable to 0 and incrementing the count value until the user's guess is right.
OUTPUT:
Comments
Post a Comment