我正在用 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 循环。
提前致谢!!!
答案 0 :(得分:2)
尝试使用 continue 语句 https://www.tutorialspoint.com/python/python_loop_control.htm#:~:text=The%20continue%20statement%20in%20Python,both%20while%20and%20for%20loops。
答案 1 :(得分:1)
Else 不是循环,但您可以使用 continue
立即进入下一个循环迭代。
另外...如果你只是想打破 else 语句,我不知道你为什么要这样做,因为在那行下面没有任何代码...