在最后一行出现空白 SyntaxError

Getting blank SyntaxError on the last line

我是编程初学者,我正在尝试使用 Twython 编写这个 Twitter 机器人作为学习练习 Python。我已经开始在最后一行收到此 SyntaxError:

     ~/tweetbot $ python tweet_test.py
File "tweet_test.py", line 32

               ^
SyntaxError: invalid syntax

我搜索了这个问题,它似乎通常与打开括号有关,或者 Python 需要更多代码。我尝试了不同的发现,比如以 sys.exit() 结尾,但我似乎总是得到 SyntaxError.

代码如下:

import sys
from twython import Twython, TwythonError


apiKey = 'letters and numbers'
apiSecret = 'letters and numbers'
accessToken = 'letters and numbers'
accessTokenSecret = 'letters and numbers'

twitter = Twython(apiKey, apiSecret, accessToken, accessTokenSecret)


try:
    searchResult = twitter.search(q='searchTerm',lang='es',result_type='recent',count='3')
    for tweet in searchResult['statuses']:
        tweeted = open('answered').readlines()
        if tweet['id_str'] in tweeted:
            print '1 tweet is already answered'
        else:
            print 'Tweet from @%s Date: %s' % (tweet['user']['screen_name'].encode('utf-8'), tweet['created_at'])
            print tweet['text'].encode('utf-8'), '\n'

            twitter.update_status(status=message, in_reply_to_status_id=tweet['id_str'])
            message = 'tweetReply'
            print "Tweeted: " + message

            answeredTweet = '"' + tweet['id_str'] + '"\n'
            f = open('answered', 'w')
            f.write(answeredTweet)

您从不提供 except: or finally: block for your try: statement。它不是可选的。

如果您实际上不想使用异常处理程序,只需删除 try: 并取消缩进整个块。