AnyCompiler
← Blog

September 8, 2026

IndentationError in Python: The Real Causes (and How to Stop Hitting It)

Unlike most languages, Python uses indentation itself to define code blocks — no curly braces, no begin/end keywords. That makes code readable, but it also means a single misplaced space can genuinely crash your program before it even runs.

def greet(name):
    print("Hello")
      print(name)
Output
  File "script.py", line 3
    print(name)
    ^
IndentationError: unexpected indent

The two real causes

In the example above, the problem is exactly that: print(name) is indented further than print("Hello"), even though both lines are meant to belong to the same function body.

The fix

def greet(name):
    print("Hello")
    print(name)

greet("Ava")
Output
Hello
Ava

Once both lines inside greet() use identical indentation, the error disappears and the function runs normally.

Most code editors can be configured to show whitespace characters and convert tabs to spaces automatically — turning that on is the single best way to stop this error before it happens. For more on how Python uses indentation to structure code, see the Conditional Statements lesson in our free Python course.