The program below should give the user four chances to guess a secret number from 1 to 10. If the guess is too high or too low, the user should be given a hint and another guess (up to the 3-guess limit). If the user picks the right number (7), the program should print a "You Win!" message and halt.
The following examples demonstrate the expected program output.
Guess a number from 1-10: 1
Too low
Guess a number from 1-10: 9
Too high
Guess a number from 1-10: 7
You win!
Thanks for playing!
Guess a number from 1-10: 1
Too low
Guess a number from 1-10: 9
Too high
Guess a number from 1-10: 5
Too low
Guess a number from 1-10: 8
Too high
Thanks for playing!
Guess a number from 1-10: 7
You win!
Thanks for playing!
The program does not work correctly. Without making any other changes, add either a continue or break statement in the right spot to produce the expected output.
num = 7
for i in range(0,4):
guess = int(input("Guess a number from 1-10: "))
if guess == num:
print("You win!")
elif guess > num:
print("Too high")
elif guess < num:
print("Too low")
print("Thanks for playing!")
1 answer
Here is the updated code:
num = 7
for i in range(0,4):
guess = int(input("Guess a number from 1-10: "))
if guess == num:
print("You win!")
break
elif guess > num:
print("Too high")
elif guess < num:
print("Too low")
print("Thanks for playing!")