Welcome to Stack Exchange, sebas!
Here's how I would implement the "try again" message:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit: # this could also just be `while True:` since the `if` will always break out
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
if guess_count == guess_limit:
print("You lost!")
break
else:
print("Try again!")
As the comment says, the while statement could actually just be an infinite loop of while True:
since the if
statement logic will always eventually break out of the loop.
UPDATE: I initially said that the while
/else
wasn't valid. Actually, it turns out that it is -- thanks, blhsing, for pointing that out. I would probably leave it out, since to me it makes the "try again" bit of the logic easier for my brain, but it's a nifty feature and could easily be used as well, like so:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit: # this could also just be `while True:` since the `if` will always break out
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
if guess_count < guess_limit:
print("Try again!")
else:
print("You lost!")