Storing test cases as a list of tuples

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

Storing test cases as a list of tuples

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores two test cases for a square function, each as (input, expected)?
A tests = (5, 25, 0, 0)
B tests = [(5, 25), (0, 0)]
2. In tests = [(5, 25), (0, 0)], what does the tuple (5, 25) represent?
A One test case: input 5, expected output 25
B Two separate inputs, 5 and 25
3. How many test cases are in tests = [(2, 4), (3, 9), (4, 16)]?
A 3
B 6
4. Which is the correct way to add a new test case with input 10 and expected 100?
A tests.append((10, 100))
B tests.append(10, 100)
5. Why store test cases in a list of tuples instead of writing a separate variable for each one?
A Python only lets you test code that uses a list
B A loop can run every test case stored in one variable

Match the Pairs

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

Left columnRight column
1tests = [(5, 25), (0, 0)] A(4, 16)
2tests.append((3, 9)) BThe second tuple is closed with a square bracket ] instead of a round bracket ), so the code has a syntax error.
3Each test case is one tuple that pairs an input with its expected output CAdds a new test case with input 3 and expected output 9 to the end of the list
4A loop can unpack each tuple in the list so every test case is run in turn D(0) is not a tuple and only holds the input, so the expected output is missing and this test case is incomplete.
5tests = [(5, 25), (0, 0] ECreates a list holding two test cases, each stored as an (input, expected) tuple
6tests = [(5, 25), (0)] Ffor x, expected in tests:

Definitions

tests = [(5, 25), (0, 0)]

Fill in the Blanks

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

1.
_____ = [(5, 25), (0, 0)]
2.
# expected is the square of the input tests = [(3, _), (0, 0)]
3.
tests = [(5, 25), (0, 0)] tests.______((4, 16))
4.
tests = [(2, 4), (3, 9)] for inp, expected in _____: print(inp, expected)
5.
tests = [(5, 25), (0, 0)] # expected output of the first test case print(tests[_][1])

Spot the Error

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

Question 1
A tests = [(5, 25) (0, 0)]
B for n, want in tests:
C print(n, want)
Question 2
A passed = 0
B tests = [(5, 25), (0, 0)
C for n, want in tests:
D if n * n == want:
E passed += 1
Question 3
A tests = [(2, 4), (3, 9)]
B tests.add((4, 16))
C for n, want in tests:
D print(n, want)
Question 4
A tests = [(2, 4), (3, 9)]
B for n, want in tests:
C got = n * n
D print(got = want)
Question 5
A tests = [(2, 4), (3, 9)]
B for test in tests:
C n = test[2]
D want = test[1]
E print(n, want)

Answer Key

Storing test cases as a list of tuples
Section A: Quick Questions
Q1
B — tests = [(5, 25), (0, 0)]
Q2
A — One test case: input 5, expected output 25
Q3
A — 3
Q4
A — tests.append((10, 100))
Q5
B — A loop can run every test case stored in one variable
Section B: Match the Pairs
Q1
tests = [(5, 25), (0, 0)] → Creates a list holding two test cases, each stored as an (input, expected) tuple
Q2
tests.append((3, 9)) → Adds a new test case with input 3 and expected output 9 to the end of the list
Q3
Each test case is one tuple that pairs an input with its expected output → (4, 16)
Q4
A loop can unpack each tuple in the list so every test case is run in turn → for x, expected in tests:
Q5
tests = [(5, 25), (0, 0] → The second tuple is closed with a square bracket ] instead of a round bracket ), so the code has a syntax error.
Q6
tests = [(5, 25), (0)] → (0) is not a tuple and only holds the input, so the expected output is missing and this test case is incomplete.
Section C: Definitions
Q1
tests = [(5, 25), (0, 0)] — A list of (input, expected) tuples. Each tuple is one test case, so a loop can run many tests stored in one variable. Add a new tuple to add a new test.
Section D: Fill in the Blanks
Q1
tests = [(5, 25), (0, 0)]
Q2
# expected is the square of the input tests = [(3, 9), (0, 0)]
Q3
tests = [(5, 25), (0, 0)] tests.append((4, 16))
Q4
tests = [(2, 4), (3, 9)] for inp, expected in tests: print(inp, expected)
Q5
tests = [(5, 25), (0, 0)] # expected output of the first test case print(tests[0][1])
Section E: Spot the Error
Q1
A — The tuples in the list are not separated by a comma, so Python raises a SyntaxError. It should be tests = [(5, 25), (0, 0)].
Q2
B — The list is missing its closing square bracket, so Python raises a SyntaxError. It should end with )] like tests = [(5, 25), (0, 0)].
Q3
B — Lists have no add() method, so this raises an AttributeError. To add a new test case tuple to a list, use tests.append((4, 16)).
Q4
D — A single = is not a comparison, so print(got = want) is an error. To check whether the actual result matches the expected result, use ==, as in print(got == want).
Q5
C — Each test tuple has only two items, at index 0 and 1, so test[2] causes an IndexError. The input is the first item, so it should be n = test[0].