Skip to content Skip to sidebar Skip to footer

Python Error "indentationerror: Expected An Indented Block"

This is my program and I am getting the following mentioned error: def main(): print 'hello' if __name__=='__main__': main() Error File 'hello.py', line 8 main() ^

Solution 1:

Your indentation is off. Try this :

def main():
    print "hello"

if __name__=='__main__':
    main()

All function blocks, as well as if-else, looping blocks have to be indented by a tab or 4 spaces (depending on environment).

if condition :
    statements  //Look at the indentation here
    ...
Out-of-block //And here

Refer to this for some explanation.

Solution 2:

NormalCode
    Indent block
    Indent block
        Indent block 2
        Indent block 2

You should do:

def main():
    print "hello"

if __name__=='__main__':
    main()

It can be either spaces or tabs.

Also, the indentation does NOT need to be same across the file, but only for each block.

For example, you can have a working file like this, but NOT recommended.

print"hello"ifTrue:
[TAB]print"a"
[TAB]i = 0
[TAB]if i == 0:
[TAB][SPACE][SPACE]print"b"
[TAB][SPACE][SPACE]j = i + 1
[TAB][SPACE][SPACE]if j == 1:
[TAB][SPACE][SPACE][TAB][TAB]print"c

Solution 3:

You probably want something like:

def main():
    print "hello"

if __name__=='__main__':
    main()

Pay attention to the indentations. Leading whitespace at the beginning of a line determines the indentation level of the line, which then is used to determine the grouping of statements in Python.

Solution 4:

Your code should look like:

def main():
    print "hello"

if __name__=='__main__':
    main()

Post a Comment for "Python Error "indentationerror: Expected An Indented Block""