Python Forum
while loops and breaks
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loops and breaks
#11
(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
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020