Python 语法错误:“继续”在循环中不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14312869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:03:19  来源:igfitidea点击:

syntaxError: 'continue' not properly in loop

pythontwittercontinuetry-except

提问by anonuser0428

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.

我特别在最后包含 continue 的原因是因为我希望程序从睡眠后停止的位置重新开始执行以保留程序状态。我需要睡眠才能遵守 twitter api 速率限制,其中 api 只允许您每小时发出一定数量的请求。因此,任何可能看到我的错误幼稚或以其他方式出现的人都请指出,或者请为我提供不使用 continue 语句的替代实现。

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

顺便说一句,我没有像另一篇文章中建议的那样混合使用制表符和空格。提前谢谢你的帮助。

采纳答案by Tim

continueis only allowed within a foror whileloop. You can easily restructure your function to loop until a valid request.

continue只允许在fororwhile循环中。您可以轻松地重构您的函数以循环直到有效请求。

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)

回答by Srikar Appalaraju

The problem might be in the way you are using continue

问题可能出在您使用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.

continue 只能在语法上嵌套在 for 或 while 循环中出现,但不能嵌套在该循环内的函数或类定义或 finally 语句中。6.1 它继续最近的封闭循环的下一个循环。