parameter: A named input to a function; threshold: A limit used for a decision; blank line: A line with no visible data
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
What is the total of 18, 22, 27 and 25? What does a file line become after int?
Build the idea
Separate reading from analysing. A reader returns a list of numbers. A counting function receives that list and a limit. This lets you test the counting rule without relying on a file every time.
For each line, strip whitespace first. If the result is empty, skip it. Otherwise convert it under the stated valid-integer contract. Skipping blank lines is not the same as validating arbitrary words.
A counting loop starts at zero, visits every item and increments only when the condition holds. Return after the loop so all values are checked. An empty list naturally returns zero.
text lines → reader → list → count function → integer
See the method
The reader ignores blank lines. All other lines must contain valid integer text. It does not validate arbitrary text. For a file containing 18, a blank line, and 25, it returns [18, 25]. Then count_high([18, 25], 25) returns 1. Equality counts as high.
def read_readings(filename):
values = []
with open(filename, "r", encoding="utf-8") as source:
for line in source:
text = line.strip()
if text != "":
values.append(int(text))
return values
def count_high(values, threshold):
count = 0
for value in values:
if value >= threshold:
count = count + 1
return count
Predict then reveal
Fresh lines are "6\n", " \n", "9\n". What list should a blank-skipping reader return?
Reveal reasoning
Strip gives "6", "", "9". Skip the empty string and convert the others. The result is [6,9]. A line "warm" would not satisfy the valid-integer contract.
Count values at least 8 in [6,8,10]. Trace value, test and counter.
Reveal reasoning
Start 0. Value 6: False, stay 0. Value 8: True, become 1. Value 10: True, become 2. Return 2 after every item has been visited.
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 both functions. Load readings.txt from lesson 6.1. Print the list, its length, and count_high(values, 25). Then test thresholds 18 and 28. Record predictions before running.
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 both functions. Load readings.txt from lesson 6.1. Print the list, its length, and count_high(values, 25). Then test thresholds 18 and 28. Record predictions before running.
Create blank_test.txt with 18, a blank line, 25, and a line containing spaces. Create empty.txt with no text. Predict and test both readers. What count does the empty list give?
Write a three-row trace for count_high([24, 25, 26], 25). Show value, the Boolean test, and the new count.
Check and explain
A return inside the loop can stop the count after the first reading.
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
Where must return count go, and why?
HL challenge when ready
Would this reader accept the word hot? Explain and choose a sensible next step.
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
Convert valid lines to integers and report invalid ones without crashing.
No imports needed.
Hint and starter code
ValueError means the text cannot be converted as requested; do not silently replace it with zero.
# ValueError means the text cannot be converted as requested; do not silently replace it with zero.
values = []
for line in ["24", "bad", "27"]:
try:
values.append(int(line))
except ValueError:
print("invalid", line)
print(values)
Filter readings within an allowed range. Add a value at each boundary.
No imports needed.
Hint and starter code
Validation checks whether data meets a rule; it does not prove the sensor was accurate.
# Validation checks whether data meets a rule; it does not prove the sensor was accurate.
readings = [-5, 24, 80]
valid = []
for reading in readings:
if 0 <= reading <= 50:
valid.append(reading)
print(valid)