Ad Space — Top Banner

IndentationError

Python Programming Language

Severity: Minor

What Does This Error Mean?

An IndentationError means the spacing at the start of a line is wrong. Python uses indentation (spaces or tabs at the beginning of lines) to understand which code belongs inside an if block, a loop, or a function. If those spaces are inconsistent or missing, Python cannot figure out the structure and stops.

Affected Models

  • Python 2.x
  • Python 3.x
  • All Python versions

Common Causes

  • Forgetting to indent the code inside an if, for, while, def, or class block
  • Mixing tabs and spaces — some lines use Tab, others use spaces, and Python gets confused
  • Accidentally removing spaces at the start of a line while editing
  • Copying code from a website or PDF that uses different spacing characters
  • An extra space or tab on a line that should be at the same level as the one above it

How to Fix It

  1. Read the error message. Python tells you the exact line number. Go to that line in your code.

    The message 'expected an indented block' means the line after a colon has no indentation at all.

  2. Make sure every line inside a block is indented by the same number of spaces. The standard in Python is 4 spaces per level.

    If the block starts with 4 spaces, every line in that block must also start with exactly 4 spaces.

  3. Turn on 'show whitespace' in your code editor. This makes tabs and spaces visible so you can spot mismatches.

    In VS Code: View → Render Whitespace. Tabs look like arrows, spaces look like dots.

  4. Convert all tabs to spaces (or all spaces to tabs) so your file is consistent. Most editors have an 'indent using spaces' setting.

    Python 3 does not allow mixing tabs and spaces in the same file. Pick one and stick with it.

  5. If the error says 'unexpected indent', a line has MORE indentation than it should. Remove the extra spaces from the start of that line.

    This often happens when you copy and paste code from somewhere else.

When to Call a Professional

IndentationErrors are always something you can fix yourself. Use an editor with visible whitespace (like VS Code) so you can see exactly where tabs and spaces are. If you are stuck, paste your code into an online Python formatter — it will fix the indentation automatically.

Frequently Asked Questions

How many spaces should I use for indentation in Python?

The official Python style guide (PEP 8) recommends 4 spaces per indentation level. Most editors default to this. The exact number does not matter as long as you are consistent throughout the whole file.

Can I use tabs instead of spaces in Python?

Yes, but you must never mix tabs and spaces in the same file. Python 3 raises a TabError if you do. The safest choice is to configure your editor to insert 4 spaces whenever you press the Tab key — then you never have to think about it.

I fixed the indentation but I still get the error — why?

There might be another indentation problem somewhere else in the file. Python stops at the first error it finds. Fix it, run again, and look for the next error message. Repeat until the program runs.