简体   繁体   English

跳出 if else 循环而不跳出 while 循环

[英]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.我正在 Python 中制作一个程序,我需要在不跳出 while 循环的情况下跳出 if else lop。

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.我想打破 else 并再次启动 if 循环,但我不能这样做,因为它打破了 while 循环。

Thanks in advance!!!!提前致谢!!!!

Else is not a loop, but you can immediately go to the next loop iteration by using continue . Else 不是循环,但您可以使用continue立即 go 到下一个循环迭代。

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...另外...如果您只想跳出 else 语句,我不确定您为什么要这样做,因为该行下方没有任何代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM