← ACARA Digital Technologies Years 7–8

IndentationError and missing colons

Python errors when a block's lines are not indented consistently, or when the if, for, while or def line that opens the block is missing its colon.

Understanding IndentationError and missing colons in ACARA Digital Technologies Years 7–8

What it does: Both lines in the while body are indented by exactly 4 spaces, so Python treats them as one block and doesn't raise an IndentationError. An if line must end with a colon. Without it Python shows a SyntaxError because it cannot tell where the block starts. Mixing tabs and spaces can look the same on screen but confuses Python. Pick one style, usually 4 spaces, and stick to it.

Common mistakes: The if line opens a block, so it must end with a colon. Without it Python raises a syntax error. It should be: if score > 5:. The if line opens a block but has no colon, so the indented line below it has nothing to belong to. It should be: if age < 13:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 482–519) and word search (Modules 420–519).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line of code is correct Python?
if score > 10if score > 10:
Matches
x = 5 print(x)
↓
Unexpected indent: print(x) is indented even though no block was opened above it. Remove the extra spaces so the line lines up with x = 5.
Applying
Meanings
Term
IndentationError
Meaning
Python errors when a block's lines are not indented consistently, or when the if, for, while or def line that opens the block is missing its colon.
Practice
def greet(name):
Testing
Gaps
if age >= 13
Check Answer
Spots
1score = 10
2if score > 5

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Golden Eagle' badge — High Flyer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line of code is correct Python?
A. if score > 10: ✓
B. if score > 10
An if line must end with a colon. Without it Python shows a SyntaxError because it cannot tell where the block starts.
2. Python says 'IndentationError: expected an indented block'. What is the most likely cause?
A. The line after the colon is not indented ✓
B. The variable name is spelled wrong
After a line ending in a colon, the next line must be indented so Python knows it belongs inside the block. A spelling mistake would give a NameError instead.
3. Which for loop is written correctly?
A. for i in range(3): print(i)
B. for i in range(3): print(i) ✓
The print(i) line belongs inside the loop, so it must be indented. Without the indent you get an IndentationError.
4. Which def line has the correct syntax?
A. def greet()
B. def greet(): ✓
A function definition must end with a colon, just like if, for and while lines.
5. A line in the middle of your program has extra spaces at the start, but it is not after a colon. What error will Python show?
A. IndentationError: unexpected indent ✓
B. IndentationError: expected an indented block
An 'unexpected indent' means a line is indented when it should not be. 'Expected an indented block' means a line is missing an indent after a colon.

Match the Pairs

if age >= 13: print('Teen') → IndentationError: expected an indented block. The line after the colon is not indented, so Python can't tell it belongs inside the if.
for i in range(3) print(i) → SyntaxError: expected ':'. The for line is missing the colon that opens the loop block.
Every line inside a block must be indented by the same amount so Python knows they belong together → while lives > 0: print(lives) lives -= 1
A def line must end with a colon, followed by an indented body → def greet(): print('Hi')
x = 5 print(x) → Unexpected indent: print(x) is indented even though no block was opened above it. Remove the extra spaces so the line lines up with x = 5.
if score > 5: print('Win') print('Done') → Mixing spaces and a tab: the last line is indented with a tab while the one above uses spaces. Use the same 4 spaces on both lines.

Key Term

IndentationError
Python errors when a block's lines are not indented consistently, or when the if, for, while or def line that opens the block is missing its colon.

Fill in the Blanks

1. age = 14 if age >= 13: print("Teen")
Hint: Type the punctuation mark that must end a line which opens an if block.
2. names = ["Ava", "Liam"] for name in names: print(name)
Hint: Type the keyword that starts a loop going through each item in a list.
3. count = 0 while count < 3: print(count)
Hint: Type the spaces that indent this line so it sits inside the while block.
4. def show_score(score): print(score) show_score(10)
Hint: Type the keyword that opens a function definition, which also needs a colon at the end of its line.
5. lives = 3 while lives > 0: lives = lives - 1
Hint: Type the punctuation mark Python needs at the end of the line that opens a while loop.

Spot the Error

Question 1
A score = 10
B if score > 5
C print("Pass")
Line B — The if line opens a block, so it must end with a colon. Without it Python raises a syntax error. It should be: if score > 5:
Question 2
A age = 12
B print(age)
C if age < 13
D print("Kid")
Line C — The if line opens a block but has no colon, so the indented line below it has nothing to belong to. It should be: if age < 13:
Question 3
A total = 0
B for n in range(3)
C total += n
Line B — The for line opens the loop block, so it needs a colon at the end. It should be: for n in range(3):
Question 4
A a = 1
B b = 5
C while a < b;
D a += 1
Line C — A semicolon was typed instead of a colon. The while line must end with a colon to open its indented block. It should be: while a < b:
Question 5
A n = 7
B if n > 5;
C print("Big")
Line B — The if line ends with a semicolon, but Python needs a colon to open the indented block. It should be: if n > 5:

Student with a class code? Join your class →