0

The following code executes as expected. However, the break function doesn't stop my code from executing and continues in an endless loop. How can I stop the operation from running and exit after break? The goal is to continue with the script every 60 seconds if the text isn't found on the page. If the text is found, perform some operations and exit.

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

I've also tried adding a boolean check:

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
Shane
  • 522
  • 1
  • 6
  • 22
  • 1
    You have only 1 `break` thus it will break only the inner `while` loop, you should break the outer `while` also using flag for instance – David Mar 22 '20 at 16:32
  • 2
    If this is the sole content of one function, you can just `return`. – phipsgabler Mar 22 '20 at 16:33
  • Try reading this post: https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops – Amiram Mar 22 '20 at 16:35
  • @phipsgabler I've tried `return` in the place of `break` but keep getting SyntaxError's regardless of where I place it. – Shane Mar 22 '20 at 16:39
  • Now that shouldn't happen -- unless you put this code at the top level (outside any `def`). – phipsgabler Mar 22 '20 at 16:42
  • Your code never breaks out of the outer `while True` loop. Can you clarify what exactly the issue is? – AMC Mar 22 '20 at 18:33

1 Answers1

0

In most languages that have thinks like break or continue statements within loops, the statement acts on the loop you're "in" at the time, rather breaking out of an entire stack of nested loops

If you're struggling to break out of the outer loop after you've executed a break on an inner loop, consider instead to control the outer loop with some boolean variable that is true to start with, but is turned false by the code when you want the outer looping to stop too; you turn the boolean false, break the inner loop, then (if the end of the outer loop doesn't happen right away after the end of the inner loop) you can test the boolean and break the outer, or you can arrange things so that the next thing that happens after the inner loop finishes is the outer loop's test is run again (and this time is false)

If instead it's pertinent to stop looping and return from the function you're in, a return statement will certainly also get you out of a nested loop, but it does have the side effect that it cannot run any code that is after the set of loops

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I've edited my original post to include what I think you are suggesting but still have the same results. What am I missing? – Shane Mar 22 '20 at 16:58