: 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.
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
Comments
Leave a comment