What would be the correct way to implement a "Try again" message for a simple guessing game?
Question:
I’m trying to implement a text that says "Try again" to appear when the player guesses incorrectly. This is an extremely bare bones "game" but I started coding yesterday and I’m trying to learn all the basic functions and methods. This is the code:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
print("You lost!")
I tried using another "else" function and another "if" function but I couldn’t figure it out.
Answers:
add import random
and set secret_number = random.randint(0,9)
you don’t need to add the ‘else’ statement at the last line
it should be just "print("You lost!")"
You can implement and ELSE statement, for continuing the loop:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
won = False
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
won = True
break
# If wrong, goes in here.
else:
# Just prints, and continues the loop
print('Try again')
# After the loop, if not won, prints lost.
if not won:
print("You lost!")
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!")
You can print 'Try again'
after checking if the player guesses correctly. To avoid printing 'Try again'
in the last guess, use an additional if
statement:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
if guess_count < guess_limit: # add these two lines
print('Try again')
else:
print("You lost!")
I’m trying to implement a text that says "Try again" to appear when the player guesses incorrectly. This is an extremely bare bones "game" but I started coding yesterday and I’m trying to learn all the basic functions and methods. This is the code:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
print("You lost!")
I tried using another "else" function and another "if" function but I couldn’t figure it out.
add import random
and set secret_number = random.randint(0,9)
you don’t need to add the ‘else’ statement at the last line
it should be just "print("You lost!")"
You can implement and ELSE statement, for continuing the loop:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
won = False
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
won = True
break
# If wrong, goes in here.
else:
# Just prints, and continues the loop
print('Try again')
# After the loop, if not won, prints lost.
if not won:
print("You lost!")
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!")
You can print 'Try again'
after checking if the player guesses correctly. To avoid printing 'Try again'
in the last guess, use an additional if
statement:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
if guess_count < guess_limit: # add these two lines
print('Try again')
else:
print("You lost!")