In: Computer Science
The code that creates this program using Python:
Your program must include:
Explanation:
Here is the code which generates a random number from 1 to 100, and then keep asking the user to guess that number until either user guesses it correctly or user reaches the limit of guessing , that is 5 times.
Code:
import random
n = random.randint(1, 101)
guesses = 0
while(True):
guess = int(input("Enter guess: "))
guesses = guesses + 1
if(guess<n):
print("Too low!")
elif (guess>n):
print("Too high!")
else:
break
if(guesses==5):
print("You lose! You only get 5 guesses.")
break
if guesses<5:
print("You Win! You took", guesses, "guesses")
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!