I'm following a tutorial on while loops that is using a number guessing game as an example. The loop is set to break after three incorrect tries and prints "You lose". I wanted to add another if statement to print after each incorrect guess (Try again), but when I did the loop breaks after the first guess instead of running through all three attempts. Prior to adding the second if statement the program ran through the entire loop correctly.
secret_number = 6
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
guess = int(input('Guess the secret number! '))
guess_count += 1
if guess == secret_number:
print('...You Won!')
if guess != secret_number:
print('Nope. Try again!')
break
else:
print('...Sorry, you failed.'
As I understand it, the break ignores the if statements and only follows the parameters set by the while command. I don't understand why adding an additional if statement kills the loop after the first attempt.
Aucun commentaire:
Enregistrer un commentaire