Lingua-e
Python

What is a IndentationError?

← Errors

An IndentationError is a specific form of SyntaxError raised in Python when the indentation of a block of code does not follow the expected pattern. Python uses indentation to define code blocks, so inconsistent spaces or tabs cause this error.

Why it happens

  • Mixing tabs and spaces for indentation in the same file.
  • Using a different number of spaces for a line that should be at the same level.
  • Accidentally removing or adding a space when editing code.
  • Copying code from another source that uses a different indentation style.

Examples

Python

Code that triggers the error

def calculate(x):
    result = x * 2
  return result

Error output

  File "app.py", line 3
    return result
IndentationError: unindent does not match any outer indentation level

How to fix it

Configure your editor to use spaces only (4 spaces per indent level is the Python convention). Enable 'show whitespace' in your editor to spot mixed tabs and spaces. Run your linter or formatter (such as ruff or black) to auto-fix indentation.

Fixed code

def calculate(x):
    result = x * 2
    return result

print(calculate(5))  # 10

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free