2017-01-01 93 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