Indentation Error from Heroku for this Python bot.

2019-09-18 15:55发布

Recently I've deployed a python bot on Heroku and every time I try to run it, this error pops up.

2016-12-28 T04:32:08.770156+00:00 app[worker.1]:File "bot.py", line 43

2016-12-28 T04:32:08.770168+00:00 app[worker.1]: else:

2016-12-28 T04:32:08.770171+00:00 app[worker.1]:^

2016-12-28 T04:32:08.770172+00:00 app[worker.1]: IndentationError: expected an indented block

Here's the code block it refers to. I do understand the error they throwing but can't see the cause? (Code was from a Git repository.)

class listener(StreamListener):
    def on_data(self, raw_data):
        try:
            retweet_ed = raw_data.lower().split('"retweeted":')[1].split(',"possibly_sensitive"')[0].replace(",", "")
            tweet_text = raw_data.lower().split('"text":"')[1].split('","source":"')[0].replace(",", "") #tweet's text
            screen_name = raw_data.lower().split('"screen_name":"')[1].split('","location"')[0].replace(",", "") #tweet's authors screen name
            tweet_sid = raw_data.split('"id":')[1].split('"id_str":')[0].replace(",", "") #tweet's id


            if not any(a_acc == screen_name.lower() for a_acc in whitelist_acc):
                if not any(acc == screen_name.lower() for acc in banned_accs):
                    if not any(a_wrds in screen_name.lower() for a_wrds in whitelist_words):
                        if not any(word in tweet_text.lower() for word in banned_words):
                            if("false" in retweet_ed):
                                #call what u want to do here
                                #for example :
                                #fav(tweet_sid)
                                #retweet(tweet_sid)
                            else:
                                pass
                                #call what u want to do here
                                #for example :
                                #fav(tweet_sid)
                                #retweet(tweet_sid)
            return True
        except Exception as e:
            print(str(e)) # prints the error msg, if u dont want it comment it out
            pass

Can someone help? Give me an eye? or do roast me XD

3条回答
祖国的老花朵
2楼-- · 2019-09-18 16:32

You need to include something in the if statement. Just type

pass

Or set a useless variable for now.

And also, indent everything by one indent I think (because of the first line, correct me if I'm wrong.)

查看更多
别忘想泡老子
3楼-- · 2019-09-18 16:44

Your code has an if statement with no instructions:

if("false" in retweet_ed):
    #call what u want to do here
    #for example :
    #fav(tweet_sid)
    #retweet(tweet_sid)

Python expects an indent, but since everything is commented out, there is none.

This is basically what the error indicates:

2016-12-28 T04:32:08.770168+00:00 app[worker.1]: else:

2016-12-28 T04:32:08.770171+00:00 app[worker.1]:^

2016-12-28 T04:32:08.770172+00:00 app[worker.1]: IndentationError: expected an indented block

You cannot have an else statement that is not following an indented block (that is, an if block, or even a for).


If you want to keep the if in plain code, add a pass statement, as for the following else:

if("false" in retweet_ed):
    #call what u want to do here
    #for example :
    #fav(tweet_sid)
    #retweet(tweet_sid)
    pass

Then, the code will not raise any IndentationError.

查看更多
贼婆χ
4楼-- · 2019-09-18 16:52

First, you cannot have no code under the if statement. That is the most likely culprit and you can fix this by adding pass in that block. If you want to test this for yourself, you can run the following very simple example and verify that the interpreter gives an error when you try to run it.

if 1 == 1:
else:
    print "This will never print."

Second, it is difficult to tell because of re-encoding on SO, but you may also have mixed spaces and tabs incorrectly. If you are using vim, you can do set list to show the invisible characters and make sure you are consistent.

查看更多
登录 后发表回答