# Python IndentationError: expected an indented block after 'if', 'for' or a function definition

You write your first `if`: if the score is 50 or more, print "You passed". It looks right. You run it, and Python stops before it even starts:

```plaintext
  File "main.py", line 3
    print("You passed")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 2
```

Here is the program:

```python
# main.py
score = 75
if score >= 50:
print("You passed")
```

Nothing is misspelled. The problem is two characters you can't see: the program is missing the spaces at the start of line 3.

## What the error means

Read the last line first:

- `IndentationError`: the problem is the **indentation**, the spaces at the start of a line.
- `expected an indented block`: after a line that ends with a colon `:`, Python expects one or more lines that are pushed in to the right. That pushed-in group is called a **block**.
- `after 'if' statement on line 2`: the line that opened the block is the `if` on line 2. (Python 3.10 and newer add this part; older versions only say `expected an indented block`.)

The two lines above it say **where** Python noticed: line 3 of `main.py`, and the `^` marks point at the start of `print`. Python reached line 3, saw that it wasn't indented, and stopped.

Because this is found while Python reads your file, before anything runs, none of your program runs at all. Not even line 1.

## Why Python cares about spaces

Most languages mark a block with curly braces. In JavaScript, the same `if` looks like this:

```javascript
if (score >= 50) {
  console.log("You passed");
}
```

Python has no braces. It uses indentation instead: every line that belongs to the `if` is indented the same amount, and the first line that isn't indented any more is outside it. So in Python, indentation is not just tidiness; it is part of the meaning of the program. (If you're still choosing a first language, we compare this and more in [Which programming language should you learn first?](https://craftstack.hashnode.dev/which-programming-language-to-learn-first))

## The fix: indent the block

Put four spaces in front of every line that belongs to the `if`:

```python
# main.py
score = 75
if score >= 50:
    print("You passed")
    print("Well done!")
print("Goodbye")
```

```plaintext
You passed
Well done!
Goodbye
```

Both indented lines belong to the `if`. `print("Goodbye")` starts at the left edge again, so it is outside the block and always runs.

Four spaces per level is the standard style in Python. Most code editors insert them for you: press Tab, or press Enter after a line that ends with `:`, and the editor indents the next line.

## The same error after `for`, `while` and `def`

Every line that ends with a colon opens a block: `if`, `elif`, `else`, `for`, `while`, `def` and others. The message names the one that is missing its block. With a loop:

```python
# main.py
for name in ["Ana", "Ben"]:
print(name)
```

```plaintext
  File "main.py", line 2
    print(name)
    ^^^^^
IndentationError: expected an indented block after 'for' statement on line 1
```

And with a function:

```python
# main.py
def greet(name):
print("Hi", name)

greet("Ana")
```

```plaintext
  File "main.py", line 2
    print("Hi", name)
    ^^^^^
IndentationError: expected an indented block after function definition on line 1
```

The fix is the same each time: indent the lines that belong to it. Everything that should run on each pass of the loop, or every time the function is called, goes four spaces in:

```python
# main.py
for name in ["Ana", "Ben"]:
    print(name)

def greet(name):
    print("Hi", name)

greet("Ana")
```

```plaintext
Ana
Ben
Hi Ana
```

Note that `greet("Ana")` is **not** indented: it's outside the function, and it's the line that calls it. If you indented it too, it would become part of the function, and nothing would ever call it.

## Four related errors

### IndentationError: unexpected indent

The opposite problem: a line is indented when nothing asked for a block.

```python
# main.py
name = "Ana"
  print(name)
```

```plaintext
  File "main.py", line 2
    print(name)
IndentationError: unexpected indent
```

This often happens when you paste code from somewhere else, or press space by accident. Move the line back to the left edge.

### IndentationError: unindent does not match any outer indentation level

The lines in a block must all line up. Here the second `print` is indented by two spaces, while the first one has four:

```python
# main.py
score = 75
if score >= 50:
    print("You passed")
  print("Well done")
```

```plaintext
  File "main.py", line 4
    print("Well done")
                      ^
IndentationError: unindent does not match any outer indentation level
```

Python can't tell whether line 4 is meant to be inside the `if` (four spaces) or outside it (no spaces). Choose one and line it up exactly.

### TabError: inconsistent use of tabs and spaces in indentation

A Tab character and four spaces can look identical on screen, but to Python they are different. If one line in a block is indented with spaces and the next with a Tab, you get:

```plaintext
TabError: inconsistent use of tabs and spaces in indentation
```

The cure is to use only spaces. Most editors have a setting to insert spaces when you press Tab, and a command to turn existing Tabs into spaces.

### A block with only a comment in it

Sometimes you start an `if` and want to fill it in later, so you leave a comment:

```python
# main.py
score = 75
if score >= 50:
    # TODO
print("done")
```

```plaintext
IndentationError: expected an indented block after 'if' statement on line 2
```

A comment doesn't count as code, so the block is still empty. Write `pass`, which means "do nothing here for now":

```python
if score >= 50:
    pass
```

## The sneaky one: no error, wrong result

Indentation can also be wrong without any error at all. Look at this version with a score of 40:

```python
# main.py
score = 40
if score >= 50:
    print("You passed")
print("Well done!")
```

```plaintext
Well done!
```

Python is happy, but the program congratulates someone who failed. `print("Well done!")` is not indented, so it isn't part of the `if` and runs every time. When a program does something "even though" a condition is false, check the indentation of that line first.

The same goes for `else`: it has to line up with its `if`. Indent it by mistake, and Python reports a `SyntaxError: invalid syntax` on the `else` line.

## A quick checklist

1. Find the line number in the message, and look at the line **above** it: does it end with a colon?
2. Indent every line that belongs to that block by four spaces.
3. Make sure all lines in a block line up exactly, and that `else` and `elif` line up with their `if`.
4. Use spaces only, never Tab characters. Let your editor handle it.
5. If the program runs but does the wrong thing, check which lines are inside the block and which are outside.

Once your blocks are in order, the next error you're likely to meet in a first program is about mixing text and numbers: [Python TypeError: unsupported operand type(s) for -: 'int' and 'str'](https://craftstack.hashnode.dev/python-typeerror-unsupported-operand-int-and-str). And the same way of reading errors works in other languages too: [How to Read an Error Message: A 3-Step Method for Python, Java, JavaScript and SQL](https://craftstack.hashnode.dev/how-to-read-an-error-message-a-3-step-method-for-python-java-javascript-and-sql).

In our course "The Toolkit", indentation comes up in the very first session, where you build a program that turns a score into a letter grade with `if`, `elif` and `else`. When you meet this error, the AI tutor doesn't fix it for you. It starts with a small hint, like asking which line the message points to and what the line above it ends with, then a more concrete hint, and shows the full solution only after two real attempts, so the fix is usually one you found yourself. You can try it in one free 20-minute session, with nothing to install: [craftstackacademy.com/trial](https://craftstackacademy.com/trial). It leads into our [Python course](https://craftstackacademy.com/courses/python).

*This article was first published on the [Craftstack Academy blog](https://craftstackacademy.com/blog/python-indentationerror-expected-an-indented-block).*

