语法错误:“继续"在循环中不正确 [英] syntaxError: 'continue' not properly in loop

查看:38
本文介绍了语法错误:“继续"在循环中不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为这个错误苦苦挣扎了一段时间,关于解释器为什么抱怨继续"似乎有不同的意见.所以我想提供下面的错误代码.

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

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

推荐答案

continue 只允许在 forwhile 循环中使用.您可以轻松地重构您的函数以循环直到一个有效的请求.

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)

这篇关于语法错误:“继续"在循环中不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆