DEV Community

Srinivas for ItsMyCode

Posted on • Originally published at itsmycode.com on

IndentationError: unexpected indent

ItsMyCode |

Python language emphasizes indentation rather than using curly braces like other programming languages. So indentation matters in Python, as it gives the structure of your code blocks, and if you do not follow it while coding, you will get an indentationerror: unexpected indent.

What are the reasons for IndentationError: unexpected indent?

IndentationError: unexpected indent mainly occurs if you use inconsistent indentation while coding. There are set of guidelines you need to follow while programming in Python. Let’s look at few basic guidelines w.r.t indentation.

*Python and PEP 8 Guidelines *

  1. Generally, in Python, you follow the four spaces rule according to PEP 8 standards.
  2. Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs.
  3. Do not mix tabs and spaces. Python disallows the mixing of indentation.
  4. Avoid trailing whitespaces anywhere because it’s usually invisible and it causes confusion.

Solving IndentationError: expected an indented block

Now that we know what indentation is and the guidelines to be followed, Let’s look at few indentation error examples and solutions.

Example 1 – Indenting inside a function

Lines inside a function should be indented one level more than the “def functionname”.

# Bad indentation inside a function

def getMessage():
message= "Hello World"
print(message)

getMessage()

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 2
    message= "Hello World"
    ^
IndentationError: expected an indented block

# Proper indentation inside a function

def getMessage():
    message= "Hello World"
    print(message)

getMessage()

# Output
Hello World
Enter fullscreen mode Exit fullscreen mode

Example 2 – Indentation inside for, while loops and if statement

Lines inside a for, if, and while statements should be indented more than the line, it begins the statement so that Python will know when you are inside the loop and when you exit the loop.

Suppose you look at the below example inside the if statement; the lines are not indented properly. The print statement is at the same level as the if statement, and hence the IndentationError.

# Bad indentation inside if statement
def getMessage():
    foo = 7
    if foo > 5:
    print ("Hello world")

getMessage()

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 4
    print ("Hello world")
    ^
IndentationError: expected an indented block
Enter fullscreen mode Exit fullscreen mode

To fix the issues inside the loops and statements, make sure you add four whitespaces and then write the lines of code. Also do not mix the white space and tabs these will always lead to an error.

# Proper indentation inside if statement
def getMessage():
    foo = 7
    if foo > 5:
        print ("Hello world")

getMessage()

# Output
Hello world
Enter fullscreen mode Exit fullscreen mode

Conclusion

The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock and ideally use a good IDE that solves the problem for you.

The post IndentationError: unexpected indent appeared first on ItsMyCode.

Discussion (0)