IndentationError: unexpected indent
Tags: python, programming, codenewbie, tutorial
Published: 2021-09-06 10:53:14

---
title: IndentationError: unexpected indent
published: true
date: 2021-09-05 10:18:57 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://fluentprogramming.com/indentationerror-unexpected-indent/
---

Fluent Programming|

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**](https://www.python.org/dev/peps/pep-0008/). 
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”. 

```python 
# 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
```

## 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.

```python 
# 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
```

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.

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

# Output
Hello world
```

## 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](https://fluentprogramming.com/indentationerror-unexpected-indent/) appeared first on [Fluent Programming](https://fluentprogramming.com).