Python Forum

Full Version: while loops and breaks
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(May-29-2018, 02:19 PM)Leonzxd Wrote: [ -> ]
(May-26-2018, 08:29 PM)gruntfutuk Wrote: [ -> ]You need to indent the break statement so it is at the same indent level as print in the final else. The final print statement should not be indented at all.

This means, when the else condition is met, correct will be printed, the loop will be exited, and you guessed the right number will be printed.

secret_num = round(3.3312, 2)
 
while True:
    user_guess = float(input("Guess the secret number: "))
    if user_guess > secret_num:
        print ("Too High!")
    elif user_guess < 3.3:
        print ("Too low!")
    else:
        print ("Great Guess")
        print ("You guessed the right number")
        break   
 
Did you mean like this?

A somewhat belated, yes, to fix the break statement being in the wrong place and executing on the first loop. (The final print statement could have been left where it was, of course.)

I also said correct rather than Great Guess, but that was of no consequence.

I'd thought it best to leave the OP to make the simple corrections (before going on to deal with other issues), but the original had a break that executed on first pass through while loop (although the
Pages: 1 2