Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntaxError: 'continue' not properly in loop

I have been struggling with this error for a while now and there seems to be different opinions regarding why the interpreter complains about the 'continue'. So I would like to provide the erroneous code below.

import tweepy
import time
def writeHandlesToFile():
    file = open("dataFile.txt","w")
    try:
        list = tweepy.Cursor(tweepy.api.followers,screen_name='someHandle',).items(100000)
        print "cursor executed"
        for item in list:
            file.write(item.screen_name+"\n")
    except tweepy.error.TweepError as e:
        print "In the except method"
        print e
        time.sleep(3600)
        continue

The reason I am particular on including the continue at the end is because I would like for the program to restart execution at the top from where it left off after the sleep in order to preserve the program state. I need the sleep in order to abide by the twitter api rate limits wherein the api only allows you to make a certain number of requests every hour. So anyone who might see my mistake naive or otherwise please do point it out or please provide me with an alternative implementation without the use of the continue statement.

BTW I do not have tabs and spaces mixed as was suggested in another post. Thank you for your help in advance.

like image 543
anonuser0428 Avatar asked Jan 14 '13 03:01

anonuser0428


People also ask

How do I fix SyntaxError continue not properly in loop?

The SyntaxError: continue not properly in loop error is raised when you try to use a continue statement outside of a for loop or a while loop. To fix this error, enclose any continue statements in your code inside a loop.

How do you continue a for loop in Python?

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.

How do you fix a broken outside loop?

The Python "SyntaxError: 'break' outside loop" occurs when we use the break statement outside of a loop. To solve the error, use a return statement to return a value from a function, or use the sys. exit() method to exit the interpreter.

Is there a continue command in Python?

Definition and Usage. The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.


2 Answers

continue is only allowed within a for or while loop. You can easily restructure your function to loop until a valid request.

def writeHandlesToFile():
    while True:
        with open("dataFile.txt","w") as f:
            try:
                lst = tweepy.Cursor(tweepy.api.followers,screen_name='someHandle',).items(100000)
                print "cursor executed"
                for item in lst:
                    f.write(item.screen_name+"\n")
                break
            except tweepy.error.TweepError as e:
                print "In the except method"
                print e
                time.sleep(3600)
like image 84
Tim Avatar answered Oct 07 '22 01:10

Tim


The problem might be in the way you are using continue

continue may only occur syntactically nested in a for or while loop, but not nested in a function or class definition or finally statement within that loop.6.1It continues with the next cycle of the nearest enclosing loop.

like image 22
Srikar Appalaraju Avatar answered Oct 06 '22 23:10

Srikar Appalaraju