1

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • _I tried using another "else" function_ Show us what you tried. We can't point out what you did wrong if you don't show us the code. – John Gordon Nov 17 '22 at 02:08

4 Answers4

2

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!")
GabrielBoehme
  • 302
  • 1
  • 11
2

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!")

Demo: https://replit.com/@blhsing/RaggedGleamingMenus

blhsing
  • 91,368
  • 6
  • 71
  • 106
0

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!")     
mister-sir
  • 124
  • 9
  • The `while-else` statement is perfectly valid in Python and is a preferred way of implementing the "You lost" condition Pythonically. Please read the [documentation](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops) of the `else` clause in a loop. – blhsing Nov 17 '22 at 02:28
  • This worked perfectly! Thank you for clarifying that the else at the end isn't needed. – sebas assaf Nov 17 '22 at 02:29
  • blhsing: oh, sheesh, you're right! That's nice and clean! I'll update my answer accordingly. – mister-sir Nov 17 '22 at 02:33
  • @blhsing I tried the code without the else "part" (what is part of a statement called by the way?) and it still printed only when the player loses. Would this work in all cases and programs or is this because of a specific reason? – sebas assaf Nov 17 '22 at 02:35
-1

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!")"