IndentationError and missing colons

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

IndentationError and missing colons

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line of code is correct Python?
A if score > 10:
B if score > 10
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
3. Which for loop is written correctly?
A for i in range(3): print(i)
B for i in range(3): print(i)
4. Which def line has the correct syntax?
A def greet()
B def greet():
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if age >= 13: print('Teen') ASyntaxError: expected ':'. The for line is missing the colon that opens the loop block.
2for i in range(3) print(i) BUnexpected 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.
3Every line inside a block must be indented by the same amount so Python knows they belong together Cdef greet(): print('Hi')
4A def line must end with a colon, followed by an indented body DIndentationError: expected an indented block. The line after the colon is not indented, so Python can't tell it belongs inside the if.
5x = 5 print(x) EMixing 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.
6if score > 5: print('Win') print('Done') Fwhile lives > 0: print(lives) lives -= 1

Definitions

IndentationError

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
age = 14 if age >= 13_ print("Teen")
2.
names = ["Ava", "Liam"] ___ name in names: print(name)
3.
count = 0 while count < 3: ____print(count)
4.
___ show_score(score): print(score) show_score(10)
5.
lives = 3 while lives > 0_ lives = lives - 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A score = 10
B if score > 5
C print("Pass")
Question 2
A age = 12
B print(age)
C if age < 13
D print("Kid")
Question 3
A total = 0
B for n in range(3)
C total += n
Question 4
A a = 1
B b = 5
C while a < b;
D a += 1
Question 5
A n = 7
B if n > 5;
C print("Big")

Answer Key

IndentationError and missing colons
Section A: Quick Questions
Q1
A — if score > 10:
Q2
A — The line after the colon is not indented
Q3
B — for i in range(3): print(i)
Q4
B — def greet():
Q5
A — IndentationError: unexpected indent
Section B: Match the Pairs
Q1
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.
Q2
for i in range(3) print(i) → SyntaxError: expected ':'. The for line is missing the colon that opens the loop block.
Q3
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
Q4
A def line must end with a colon, followed by an indented body → def greet(): print('Hi')
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
age = 14 if age >= 13: print("Teen")
Q2
names = ["Ava", "Liam"] for name in names: print(name)
Q3
count = 0 while count < 3: print(count)
Q4
def show_score(score): print(score) show_score(10)
Q5
lives = 3 while lives > 0: lives = lives - 1
Section E: Spot the Error
Q1
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:
Q2
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:
Q3
B — The for line opens the loop block, so it needs a colon at the end. It should be: for n in range(3):
Q4
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:
Q5
B — The if line ends with a semicolon, but Python needs a colon to open the indented block. It should be: if n > 5: