Answer to Question #183845 in Python for Shehry

Question #183845

: Write a small program that plays a simple number-guessing game. The user will try to guess the secret number until they get it right. That means it will keep looping as long as the guess is different from the secret number. You must store the secret number in a variable, and use that variable throughout. The secret number itself must not appear in the program at all, except in the one line where you store it into a variable. Sample output is as follows:

 

I have chosen a number between 1 and 10. Try to guess it.

Your guess: 5

That is incorrect. Guess again.

Your guess: 4

That is incorrect. Guess again.

Your guess: 8

That is incorrect. Guess again.

Your guess: 6

That's right! You guessed it.


1
Expert's answer
2021-04-22T10:30:01-0400
import random

a = int(input("Enter the lower limit of the number "))
b = int(input("Enter the upper limit of the number "))
true_number = random.randint(a,b)
print("I have chosen a number between",a,"and",b,". Try to guess it.")


while True:
    player_number = int(input("Your guess: "))
    if player_number != true_number:
        print("That is incorrect. Guess again.")
    else:
        print("That's right! You guessed it.")
        break

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS