index: An item position starting at zero in Python; boundary test: A test at or near a decision limit; expected result: The result predicted before a program runs
55-minute lesson sequence
0–5 · Answer the retrieval alone. Compare one reason.
5–15 · Teacher models the example. Students predict each next step.
15–40 · Build, trace and test the main tasks. Work alone first; compare after a checkpoint.
40–50 · Check results. Explain a test and repair one error. Use the extension only if ready.
50–55 · Complete the exit alone. Save the evidence for the next lesson.
Try before notes
For [18, 22, 27, 25], what is the index of the first value at least 25?
Build the idea
A linear search checks items in order. For a first-match search, return immediately when a match is found. If every item fails, return the agreed not-found value after the loop. This differs from a count, which must continue after each match.
Python list indices start at zero. Returning an index tells the caller where the match was found, not what its value was. Use separate names for index and reading to avoid mixing them up.
If −1 means not found, test for it before indexing the list. In Python values[-1] accesses the final item of a nonempty list, so using the sentinel as a real index can silently produce a wrong answer.
index 0 → match? → yes: return 0
no → next index
end of list → return -1
See the method
find_first_high returns an index, or -1 when no value is high. For [20, 24, 25], the checks at indices 0 and 1 fail. The check at index 2 succeeds, so the function returns 2. A return ends this function call.
def find_first_high(values, threshold):
for index in range(len(values)):
if values[index] >= threshold:
return index
return -1
Predict then reveal
Find the first index with value>=12 in [8,13,12].
Reveal reasoning
Index 0 holds 8: fail. Index 1 holds 13: match, return 1. Do not return 13, and do not continue to the later 12 when the task asks for first match.
The search returns -1 for [4,5]. Why is printing values[result] wrong?
Reveal reasoning
values[-1] is 5, the last item. It would falsely look like a matched reading. Branch on result==-1 and print the not-found message instead.
Work one step together
Read the first task. Each learner predicts the first step privately. Compare the method, then complete one intermediate row or one code iteration together. Do not display a finished workbook answer.
Practice
Build find_first_high. Test [18, 22, 27, 25], [24], [25], [26] and [] with threshold 25. For each test, record the expected index and the actual index.
Your turn
Complete the remaining steps of task 1 alone, then the next tasks. Predict before running code. Save your own evidence before comparing.
Practice
Build find_first_high. Test [18, 22, 27, 25], [24], [25], [26] and [] with threshold 25. For each test, record the expected index and the actual index.
Use the function with your loaded file. Print no high reading if it returns -1. Otherwise print the index and the reading. Explain why the -1 check must happen before reading values [index].
A student changes >= to >. Which one of the single-item tests proves the error? State expected and faulty results.
Check and explain
A not-found sentinel is a signalling value, not permission to access that list position.
Point to the first step, row, condition or requirement that supports your answer. If results differ, find the first different state and repair one cause.
Show what you know
Why is [25] a stronger test than [30] for an equality mistake at threshold 25?
HL challenge when ready
Can you use binary search on the original readings to find 25 without changing anything?
Week 6 homework
Show working. Keep code and output. Stop at 25 minutes and mark where you reached.
Without running code first, predict the report for a new file containing 24, 25, 26, a blank line and 19. Use limit 25. Then, if a computer is available, test and compare. Otherwise trace the function on paper.
A check history already has three lines. A program appends one line each run. It runs twice. How many lines are there? What would change if it used w each time?
Write one test that catches > used instead of >= at limit 25. State the input and expected answer.
Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.
A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.
Prompt 1
Return the first reading above the limit. Test a reading exactly at the limit.
No imports needed.
Hint and starter code
enumerate() gives index and value together; equality is not above.
# enumerate() gives index and value together; equality is not above.
readings = [24, 28, 30]
limit = 28
for index, reading in enumerate(readings):
if reading > limit:
print(index, reading)
break
Count all hot readings without stopping at the first.
No imports needed.
Hint and starter code
Keep the increment inside the condition but do not use break.
# Keep the increment inside the condition but do not use break.
readings = [29, 24, 30, 28]
count = 0
for reading in readings:
if reading > 28:
count += 1
print(count)