Guess & Win is an interactive computer game that allows user to guess a random n
ID: 3693152 • Letter: G
Question
Guess & Win is an interactive computer game that allows user to guess a random number between 1 and 10. During the play, the program will give hints based on the user input, as to whether it was too high or too low The game will stop when user correctly guess the number. Write a Python script which asks user to guess a generated random number between 1 and 10. Display following messages based on user input. If user input is less than the generated random number Too low ' If user input is greater than the generated random number: Too high ! If user input is same as the generated random number That ' s it ! Save the script as "lab5_chcckpoint3.py" in cs104lab5 directory.Explanation / Answer
import random
guessVar = random.randint(1,10) #Generate a random value between 1 and 10
#Initialize variable
userVar = 0
firstCycle = 0
#Game start
print("Try to guess a number between 1 and 10") #this is out of the while loop so only prints one time
while guessVar != userVar:
if firstCycle == 0: #This condition is True the first time, at the end of the loop the variable firstCycle is change it
userVar = input("What's your first guess?") #save user input in the variable userVar
else:
userVar = input("What's your next guess?") #save user input in the variable userVar
#This block compare the user input and the random value
if userVar<guessVar:
print("Too low!")
elif userVar>guessVar:
print("To high!")
else: #if guessVar == userVar user win
print("That it!")
firstCycle=1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.