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:
File "main.py", line 3
print("You passed")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 2Here is the program:
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 theifon line 2. (Python 3.10 and newer add this part; older versions only sayexpected 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:
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?)
The fix: indent the block
Put four spaces in front of every line that belongs to the if:
score = 75
if score >= 50:
print("You passed")
print("Well done!")
print("Goodbye")You passed
Well done!
GoodbyeBoth 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:
for name in ["Ana", "Ben"]:
print(name) File "main.py", line 2
print(name)
^^^^^
IndentationError: expected an indented block after 'for' statement on line 1And with a function:
def greet(name):
print("Hi", name)
greet("Ana") File "main.py", line 2
print("Hi", name)
^^^^^
IndentationError: expected an indented block after function definition on line 1The 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:
for name in ["Ana", "Ben"]:
print(name)
def greet(name):
print("Hi", name)
greet("Ana")Ana
Ben
Hi AnaNote 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.
name = "Ana"
print(name) File "main.py", line 2
print(name)
IndentationError: unexpected indentThis 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:
score = 75
if score >= 50:
print("You passed")
print("Well done") File "main.py", line 4
print("Well done")
^
IndentationError: unindent does not match any outer indentation levelPython 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:
TabError: inconsistent use of tabs and spaces in indentationThe 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:
score = 75
if score >= 50:
# TODO
print("done")IndentationError: expected an indented block after 'if' statement on line 2A comment doesn't count as code, so the block is still empty. Write pass, which means "do nothing here for now":
if score >= 50:
passThe 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:
score = 40
if score >= 50:
print("You passed")
print("Well done!")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
- Find the line number in the message, and look at the line above it: does it end with a colon?
- Indent every line that belongs to that block by four spaces.
- Make sure all lines in a block line up exactly, and that
elseandelifline up with theirif. - Use spaces only, never Tab characters. Let your editor handle it.
- If the program runs but does the wrong thing, check which lines are inside the block and which are outside.
