繁体   English   中英

在多个 While True 循环中正确使用 break

[英]Proper usage of break in multiple While True loops

以下代码按预期执行。 但是,break 函数不会阻止我的代码执行并继续无限循环。 如何在break后停止运行并退出? 如果在页面上找不到文本,目标是每 60 秒继续执行一次脚本。 如果找到文本,则执行一些操作并退出。

while True:
    #operation to scrape data from page
    while True:
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        startTime = '11:59:00'
        #if it's before 11:59
        if current_time < startTime:
            # if 9:30 doesn't appear on the page, wait 60 and try again.
            if str(soup).find("9:30") == -1:
                # wait 60 seconds,
                time.sleep(60)                    
                # continue with the script,
                continue               
            else:
                #9:30 Appears on the page so continue
                #PerformFunction()                    
                print('Time found. Performing operation...') 
                time.sleep(10)                     
                break                                                            
        else:
        # It's after 11:59
            #if 4:30 doesn't appear on the page try again in 60                
            if str(soup).find("4:30") == -1:
                # wait 60 seconds,
                time.sleep(60)                    
                # continue with the script,
                continue                
            else:
                #4:30 Appears on the page so continue
                #PerformFunction()                    
                print('Time found. Performing operation...')  
                time.sleep(10)                  
                break

我也试过添加一个布尔检查:

checker = True
while True and checker:
    #operation to scrape data from page
    while True:
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        startTime = '11:59:00'
        #if it's before 11:59
        if current_time < startTime:
            # if 9:30 doesn't appear on the page, wait 60 and try again.
            if str(soup).find("9:30") == -1:
                # wait 60 seconds,
                time.sleep(60)                    
                # continue with the script,
                continue               
            else:
                #9:30 Appears on the page so continue
                #PerformFunction()                    
                print('Time found. Performing operation...') 
                time.sleep(10)  
                checker = False                   
                break 
                #I've also tried return in the place of break but get SyntaxError                                                           
        else:
        # It's after 11:59
            #if 4:30 doesn't appear on the page try again in 60                
            if str(soup).find("4:30") == -1:
                # wait 60 seconds,
                time.sleep(60)                    
                # continue with the script,
                continue                
            else:
                #4:30 Appears on the page so continue
                #PerformFunction()                    
                print('Time found. Performing operation...')  
                time.sleep(10)
                checker = False                  
                break
                #I've also tried return in the place of break but get SyntaxError

在大多数在循环中使用breakcontinue语句的语言中,该语句作用于您当时“进入”的循环,而不是从整个嵌套循环堆栈中跳出

如果在内循环上执行了break后,您正在努力摆脱外循环,请考虑使用一些布尔变量来控制外循环,该变量开始时为真,但在以下情况下被代码变为假您也希望外循环停止; 您将布尔值设为 false,中断内循环,然后(如果在内循环结束后外循环没有立即结束)您可以测试布尔值并中断外循环,或者您可以安排这样的事情内循环完成后发生的下一件事是再次运行外循环的测试(这次是假的)

相反,如果停止循环并从您所在的函数返回是相关的,则 return 语句肯定也会让您摆脱嵌套循环,但它确实具有副作用,即它无法运行在集合之后的任何代码循环

暂无
暂无

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

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