2017-01-01 78 views
-1

祝你编码器所有的新年快乐! 我在编译早期的heroku上编写Python时遇到了问题,该问题抛出了缩进错误并被此线程回答。 Python Bot indentation error, solved 无论如何,在你们的帮助下,我通过了上面提到的错误,但弹出一个奇怪的语法错误。从控制台产生Python语法错误与Tweepy

banned_accs = ['@bbc' ,'@cnn'] 

for tweet in tweepy.Cursor(api.search, q='#test').items(): 

    if not any(acc == tweet.user.screen_name for acc in banned_accs): 
     tweet.retweet() 
     print('Retweeted the tweet') 
     # Favorite the tweet 
     tweet.favorite() 
     print('Favorited the tweet') 
     # Follow the user who tweeted 
     tweet.user.follow() 
     print('Followed the user') 
     sleep(5)  
    else: 
     pass 

except tweepy.TweepError as e: 
print(e.reason) 

except StopIteration: 
    break 

错误是

except tweepy.TweepError as error: 
    ^
SyntaxError: invalid syntax 

有人可以帮我想出解决办法?提前致谢。

+1

你似乎并不在你的代码中的'try'内缩进。这需要超出您期望可能抛出异常的代码部分,并且所有内容都应在该块内缩进。这是Python中的标准,与'tweepy'无关。 – roganjosh

+0

你的尝试块在哪里启动? –

+0

我自己犯的无意义错误。对不起,我不得不把它放在这里。谢谢你们的回复。投票删除此问题。 – AACaN

回答

1

您有except块,但我没有看到try块。也许你没有在整个问题中包含你的整个代码。

很可能您在代码的上面某处使用了try关键字,或者您忘记使用try块。 except应该与try具有相同的缩进,如果有try块或者您忘记了try块,则将其声明在可能引发异常的代码块之上。

而且print(e.reason)except

+0

代码中没有'try'。 – Barmar

+0

Ofc。错过一件简单的事情。谢谢 – AACaN

+0

是的代码没有试图捕捉除了。我自己犯的错误:/ – AACaN