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

时间:2021-06-16 04:48:57

标签: python python-3.x if-statement while-loop break

我正在用 Python 编写一个程序,我需要跳出 if else lop 而不跳出 while 循环。

当前代码(仅以不完整代码为例。不是完整代码。):

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

我想中断 else 并再次启动 if 循环,但我不能这样做,因为它跳出了 while 循环。

提前致谢!!!

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

Else 不是循环,但您可以使用 continue 立即进入下一个循环迭代。

另外...如果你只是想打破 else 语句,我不知道你为什么要这样做,因为在那行下面没有任何代码...

相关问题