← ACARA Digital Technologies Years 9–10

Looping through test cases and printing PASS or FAIL

Loops over each (input, expected) tuple in tests, unpacking it into two variables. Then you call the function, compare actual to expected, and print PASS, or FAIL with both values.

Understanding Looping through test cases and printing PASS or FAIL in ACARA Digital Technologies Years 9–10

What it does: The == operator checks whether the function's actual result equals the expected result, and PASS is printed only when they match. The for loop takes one tuple from tests each time and unpacks it into two variables: value and expected. Use == to compare two values. A single = assigns a value and causes a syntax error inside an if. FAIL belongs in the else branch, so it only runs when the comparison is False. The check is actual == expected, and 7 does not equal 10, so the test fails. Being bigger than the input doesn't matter.

Common mistakes: A single = assigns a value, so it cannot compare actual with expected. The test needs the comparison operator ==: if actual == expected:. An if statement must end with a colon. The line should be: if actual == expected:.

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 591–622) and word search (Modules 536–622).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which loop header unpacks each (input, expected) tuple in a list called tests?
for value in expected, tests:for value, expected in tests:
Matches
for value, expected in tests: actual = double(value) if actual = expected: print("PASS")
↓
A single = was used in the if condition. It assigns a value instead of comparing, which causes a SyntaxError. It should be ==.
Applying
Meanings
Term
for value, expected in tests:
Meaning
Loops over each (input, expected) tuple in tests, unpacking it into two variables. Then you call the function, compare actual to expected, and print PASS, or FAIL with both values.
Practice
def double(n):
Testing
Gaps
for value, expected tests:
Check Answer
Spots
3 actual = square(value)
4 if actual = expected:

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

The 'Rhinoceros' badge — Thick-Skinned Coder, 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 loop header unpacks each (input, expected) tuple in a list called tests?
A. for value, expected in tests: ✓
B. for value in expected, tests:
The for loop takes one tuple from tests each time and unpacks it into two variables: value and expected.
2. Inside the loop, how do you get the actual result of the function double() for the test input?
A. actual = double(expected)
B. actual = double(value) ✓
You give the function the input (value). The expected variable holds the answer you are hoping for, so it is only used for comparing.
3. Which line checks whether a test passed?
A. if actual == expected: ✓
B. if actual = expected:
Use == to compare two values. A single = assigns a value and causes a syntax error inside an if.
4. A test loop prints PASS when actual equals expected. When is FAIL printed?
A. Every time the loop starts a new test
B. In the else branch, when actual does not equal expected ✓
FAIL belongs in the else branch, so it only runs when the comparison is False.
5. Which FAIL message is most useful for finding a bug?
A. print('FAIL', value, 'expected', expected, 'got', actual) ✓
B. print('FAIL')
Printing the input, expected and actual values shows you exactly which test broke and how the result was wrong.

Match the Pairs

for value, expected in tests: → Loops through the list of test tuples and unpacks each one into an input value and an expected result
actual = double(value) → Calls the function with the test input and stores the real result to compare with the expected result
Print PASS when the actual result matches the expected result → if actual == expected: print("PASS")
On a failure, print both values so you can see what went wrong → else: print("FAIL", expected, actual)
for value, expected in tests: actual = double(value) if actual = expected: print("PASS") → A single = was used in the if condition. It assigns a value instead of comparing, which causes a SyntaxError. It should be ==.
for value in tests: actual = double(value) if actual == expected: print("PASS") → The loop only has one variable, so the whole (input, expected) tuple goes into value and expected is never defined. It should be for value, expected in tests.

Key Term

for value, expected in tests:
Loops over each (input, expected) tuple in tests, unpacking it into two variables. Then you call the function, compare actual to expected, and print PASS, or FAIL with both values.

Fill in the Blanks

1. for value, expected in tests: actual = double(value)
Hint: Type the keyword that links the two loop variables to the list of test cases.
2. for value, expected in tests: actual = double(value)
Hint: Type the variable name that receives the expected result from each tuple.
3. for value, expected in tests: actual = double(value)
Hint: Type the variable holding the test input that is passed into the function.
4. for value, expected in tests: actual = double(value) if actual == expected:
Hint: Type the operator that checks whether the two results are equal.
5. print("PASS") else: print("FAIL", actual, expected)
Hint: Type the keyword for the branch that runs when the test does not match.

Spot the Error

Question 1
A tests = [(2, 4), (3, 9)]
B for value, expected in tests:
C actual = square(value)
D if actual = expected:
E print("PASS")
Line D — A single = assigns a value, so it cannot compare actual with expected. The test needs the comparison operator ==: if actual == expected:
Question 2
A for value, expected in tests:
B actual = double(value)
C if actual == expected
D print("PASS")
E else:
F print("FAIL", actual)
Line C — An if statement must end with a colon. The line should be: if actual == expected:
Question 3
A for value in tests:
B actual = square(value)
C if actual == expected:
D print("PASS")
Line A — Each test is a tuple of (input, expected). The loop must unpack both parts: for value, expected in tests: Otherwise expected is never set inside the loop.
Question 4
A tests = [(2, 3), (7, 8)]
B for value, expected in tests:
C actual = add_one(value)
D if actual == value:
E print("PASS")
Line D — The actual result must be compared with the expected result, not the input value. It should be: if actual == expected:
Question 5
A for value, expected in tests:
B actual = triple(expected)
C if actual != expected:
D print("FAIL", value)
E else:
F print("PASS")
Line B — The function must be called with the test input, value, to get the actual result. Passing expected would not test the function properly. It should be: actual = triple(value)

Student with a class code? Join your class →