【问题标题】:Break out of if else loop without breaking out of while loop跳出 if else 循环而不跳出 while 循环
【发布时间】:2021-08-31 21:49:33
【问题描述】:

我正在用 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 循环。

提前致谢!!!!

【问题讨论】:

  • if..else 不是循环......

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


【解决方案1】:

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

另外...如果您只是想跳出 else 语句,我不确定您为什么这样做,因为该行下方没有任何代码...

【讨论】:

  • 好吧,等一下,在 cmets 中问新问题不好。此外,当冷却时间发生时,您应该花一些时间来重构您的代码,有很多更好的方法可以完成您想要完成的工作。玩得开心编程!
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2017-06-16
  • 2015-04-08
  • 2020-01-03
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
相关资源
最近更新 更多