Python 2.7.3 IndentationError: expected an indented block - cant find mistake
Solution 1
Be careful! You're mixing tabs and spaces for indenting.
Often a tab is displayed as the equivalent of 8 spaces. So, when using the common practice of 4 spaces it looks like 2 levels of indentation, but is really only one.
When I examine your code in the editor, I can see that you've got in at least two places. Replace those tabs with 4 spaces.
Solution 2
As it says, you have an indentation error. Line 25 should be corrected to this:
def main():
...
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
Solution 3
You have to indent the line after a for
block. Your code should look like:
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
Solution 4
Your code must be like this:
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
It's the same for all other for's.
Sergey Samusev
Updated on July 14, 2020Comments
-
Sergey Samusev almost 4 years
I am new to python and trying to submit my HW on Coursera Data Science course. The environment there is VM running Python 2.7.3, the file tweet_sentiment.py I am trying to run has the following script within it:
import sys import json def hw(): print 'Hello, world!' def lines(fp): print str(len(fp.readlines())) def main(): sent_file = open(sys.argv[1]) tweet_file = open(sys.argv[2]) # hw() # lines(sent_file) # lines(tweet_file) myfile = open(sys.argv[1], 'r') lines = myfile.readlines() mydict = {} for line in lines: key, value = line.split("\t") mydict[key] = int(value) twit_file = open(sys.argv[2], 'r') twit_lines = twit_file.readlines() mylist = [] for line in twit_lines: mylist.append(json.loads(line)) for listik in mylist: twit_value = 0 twit_text = listik["text"] twit_words = twit_text.split() for word in twit_words: if word in mydict: twit_value = twit_value + 1 print float(twit_value) if __name__ == '__main__': main()
When running $ python tweet_sentiment.py I am getting the following error:
File "tweet_sentiment.py", line 25 key, value = line.split("\t") ^ IndentationError: expected an indented block
Thanks for any hints! Sergey
-
Levon about 11 years+1 Just to add to what @gnibbler says, PEP8 warns about just this, and recommends the use of spaces over tabs. (4 spaces per indentation level)
-
Sergey Samusev about 11 yearsThanks! Sorry for posting this mess,I looked like I am asking really basic question. Indentation was actually well organized in my file, but when copying it to post, formatting got lost and I did not check that. Eventually my file was successfully executed by the course script which checks assignment and results went out right. I am not sure why it did not work when running on VM. Thanks for prompt response!
-
Sergey Samusev about 11 yearsThanks! Sorry for posting this mess,I looked like I am asking really basic question. Indentation was actually well organized in my file, but when copying it to post, formatting got lost and I did not check that. Eventually my file was successfully executed by the course script which checks assignment and results went out right. I am not sure why it did not work when running on VM. Thanks for prompt response!
-
kirbyfan64sos about 11 years@SergeySamusev: You're welcome! The error was an indentation error. Might have had something to do with...well...I don't really know. All I can say is...good luck! Maybe the indentation was like one space off or didn't correlate with the rest of the file. I make that seemingly novice mistake all the time.