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
You need to include something in the if statement. Just type
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.)
Your code has an
if
statement with no instructions:Python expects an indent, but since everything is commented out, there is none.
This is basically what the error indicates:
You cannot have an
else
statement that is not following an indented block (that is, anif
block, or even afor
).If you want to keep the
if
in plain code, add apass
statement, as for the followingelse
:Then, the code will not raise any
IndentationError
.First, you cannot have no code under the
if
statement. That is the most likely culprit and you can fix this by addingpass
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.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.