Search code examples
pythonfunctionloopscontinue

Pass 'continue' from inside a function to the outer loop in Python


I hope i can express myself accordingly, as I'm quite new to programming and I'm sure the answer is quite simple but I can't get there... :) So I have a 'for' loop that executes some functions. Each function has an 'if' statement in it and only does something, if the conditions allow so. What I want is the loop to reset, as soon as a function's 'if' statement is satisfied.

Edit: I messed up the function :( thx for your answers so far ^^ An example of what i wish would work:

def do_something():
   if something == True:
      do_some_stuff
      return continue

while i < 999:
   do_something()
   do_something2()
   do_something3()
   

The 'return continue' is the part that doesn't work as I wish and I can't find a solution for that (maybe I don't know what exactly to google for)


Solution

  • You can have 'do_something' return a boolean

    def do_something():
       if something is True:
          do_some_stuff
          return True
       return False
    
    while i < 999:
       if do_something():
          continue
       do_something2()
       do_something3()