ProjectStack
python

IndentationError: unexpected indent

A line is indented more than Python expected. Python uses indentation to define code blocks, so every extra space or tab that doesn't belong to a block is an error.

Common causes

  • A line is accidentally indented when it should be at the top level
  • Mixing tabs and spaces — one editor used tabs, another used spaces
  • Copy-pasting code from a website or document that added extra leading spaces
  • An extra level of indentation after a block that's already closed

How to fix it

  1. Check the indented line and remove extra leading spaces or tabs
  2. Use consistent indentation throughout — 4 spaces per level is the Python standard (PEP 8)
  3. Run your editor's 'convert tabs to spaces' feature to eliminate mixed whitespace
  4. In your editor, enable 'show whitespace' to make invisible characters visible

Example

File "app.py", line 3 x = 1 ^ IndentationError: unexpected indent

A line is indented inside the top-level scope where no block is open

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.