Search code examples
pythonpython-3.xif-statementwhile-loopbreak

Break out of if else loop without breaking out of while loop


I was making an program in Python and I needed to break out of if else lop without breaking out of while loop.

Current code (incomplete code just for example. Not full code.):

class foo:
  def __init__(self, time1, link)
        while self.isItNow == False:
            self.current_time = self.now.strftime("%H:%M")
            print(self.current_time)
            if str(self.current_time) == self.time1:
                webbrowser.open(self.link, new=1)
                self.isItNow = True
            else:
                print("Current Time =", self.current_time,". Retrying again in 5 seconds")
                time.sleep(5)
                break

I wanted to break the else and start the if loop again but I can't do it because it breaks out of the while loop.

Thanks in advance!!!!


Solution

  • Else is not a loop, but you can immediately go to the next loop iteration by using continue.

    Also... If you just want to break out of the else statement, I'm not sure why you do because do don't have any code below that line...