Julliard
Julliard

Reputation: 553

Break out of while True loop with function

So I have a while true loop -

while True:
  getStatus()
  print('Ended')

I hope to be able to break out of it if answer is 999. this is what I tried:

def getStatus():
  answer = input('What is the box ID? ')
  if answer == 999:
    return False 
  elif type(answer) == int:
    boxId = answer + 1
    print(boxId)

However even when the input is '999' it loops back and asks 'What is the box ID? ' again.

How do I get out of the while true loop?

Upvotes: 1

Views: 14327

Answers (4)

Rohan Rawat
Rohan Rawat

Reputation: 31

You may change it to -

while True:
  if(getStatus()==False):
      print('Ended')
      break

By this you can use returned value to break out of infinite loop.

Upvotes: -2

Mohamed Jeafer
Mohamed Jeafer

Reputation: 21

you can add if statement before get_status() that will check if it's true and break and in the get_status function you will have to return true to break

def getStatus():
  answer = input('What is the box ID? ')
  if answer == 999:
    return True
  elif type(answer) == int:
    boxId = answer + 1
    print(boxId)


while True:
  if getStatus():
     print('Ended')
     break

Upvotes: 1

JoeBlast
JoeBlast

Reputation: 1

def selectedCountry():
    while True:
        country = input("Enter country of which you want to check pictures HR, RS, RO:  ").upper()
        if country == str("HR") or country == str("RO") or country == str("RS"):
            break
        else:
            print("Please enter HR or RO or RS: " + "you wrote: " + country)

Why while True is working outside of a function and inside the same problem with asking again

Enter country of which you want to check pictures HR, RS, RO:  >? hr
Enter country of which you want to check pictures HR, RS, RO:  

Upvotes: -1

Blckknght
Blckknght

Reputation: 104692

Your while loop keeps looping because that's exactly what you've told it to do. After the function you call from its body returns, you ignore the return value, unconditionally print "Ended" and then do it all again, since the condition on the loop is obviously still truthy.

If you want the function to control if the loop keeps going, you should use its return value as the condition in the loop, with something like this:

running = True
while running:
    running = getStatus()
print("Ended") # move this outside the loop!

This requires that getStatus returns a truthy value when you want to keep looping and a falsey value when you want to stop. Your current implementation of that function doesn't do that. It returns False when 999 is entered, but doesn't explicitly return anything if you give other input (which in Python is equivalent to returning None). Since both False and None are falsey, the code above won't actually work (you could patch it with something like running = getStatus() is None, but that would be horrible). You should change the function to have an explicit return statement in all of its branches (including the case for non-integer inputs, where it doesn't go into either your if or elif blocks).

If the loop and the function's logic are tightly intertwined, it might make sense to move the loop into the function itself, rather than having it be separate and needing to use a return value to signal when to stop. In a single function, you can use break to exit a loop directly:

def getStatus():
    while True:
        answer = input('What is the box ID? ')
        if answer == 999:
            break
        elif isinstance(answer, int): # isinsance() is better than type(...) == ...
            boxId = answer + 1
            print(boxId)
        else:
            print("I'm sorry, I didn't understand that.") # still good to handle this case

Upvotes: 4

Related Questions