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

Popular posts from this blog

PYTHON PROGRAM THAT USES FOR LOOP TO PRINT THE NUMBERS 8,11,14,17,20,.........,83,86,89

PYTHON PROGRAM THAT ASKS THE USER TO ENTER TWO STRINGS OF SAME LENGTH AND WILL ALTERNATE THE CHARACTERS OF THE TWO STRINGS.FOR EXAMPLE: abcde and ABCDE THEN THE PROGRAM WILL PRINT AaBbCcDdEe

PYTHON PROGRAM THAT ASKS USER FOR LARGE INTEGERS AND INSERTS COMMAS INTO IT ACCORDING TO THE STANDARD AMERICAN CONVENTION FOR COMMAS IN LARGE NUMBERS