If you need to break the loop from "inside" the function, there are several ways of doing it:
- return True/False from the function, and check the return value in the loop
- raise a special exception from the function and catch it inside the loop
Examples:
# example 1
def function():
if cloudy and not umbrella:
print "no good to stay outside"
return False
return True
while nice_weather:
if not function():
break
# example 2
class RunHome(Exception):
pass
def function():
if thunderstorm:
raise RunHome()
while enjoying:
try:
function()
except RunHome as re:
break
Depending on what actually the function and the loop do, some other techniques may be suitable as well.
breakHowever, I would write this code differently, flag = True while flag: "do stuff" if blah blah: flag = Falsebreakto that if. I think you meant something else so show us the real code because this example is most likely not what you intended it to beifcondition actually insidefunction()? Because that's a more difficult and more interesting question than the one posed.breakshould do what you want. If it doesn't post a minimal reproducible example of actual Python code that reproduces your issue.