1.Why should I care?
Summaries are only useful if they reveal problems. A loop that counts readings over a limit, tracks the extremes and flags impossible values turns a wall of numbers into a decision: fine, inspect, or replace the sensor.
- ◇ Combine a loop and an if to count readings that meet a condition.
- ◇ Find the maximum and minimum with a loop, starting from the first reading.
- ◇ Flag implausible readings (glitches) and explain why a mean alone can mislead.
2.Discover
Open the Smart Greenhouse at Level 2 ('Read the data'). Find the glitch in the ten noisy readings and the mean without it. While you work, write the loop you are running in your head: what do you count, what do you compare, what do you skip?
Can't use the interactive? Read a description instead
The Smart Greenhouse Level 2 task gives ten thermometer readings, one of them an obvious glitch, and asks for the mean without it. On paper: list the readings, cross out any outside a plausible band, add the rest and divide by how many remain. In this lesson's example, 21.4, 21.6, −40.0, 21.9, 22.1, 85.0, 22.0 has mean 22.0, max 85.0, min −40.0 and 2 glitches.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A loop runs if r > 55: over = over + 1 for each reading r in [62.7, 51.5, 58.6, 63.0, 54.8, 55.0] (°C), starting from over = 0. What is over at the end?
Practice 2
To find the hottest reading in a loop, what should hottest start as?
4.Apply it
Extend your log program: report the mean of only the plausible readings, the max and min, and the number of glitches. Finish the Smart Greenhouse Level 2 task and check your program gives the same mean without the glitch.
5.Understand
Count with an if inside a loop
readings_c = [21.4, 21.6, -40.0, 21.9, 22.1, 85.0, 22.0]
hottest = readings_c[0]
coldest = readings_c[0]
glitches = 0
for r in readings_c:
if r > hottest:
hottest = r
if r < coldest:
coldest = r
if r < -10 or r > 50:
glitches += 1
print(hottest, coldest, glitches) # 85.0 -40.0 2Why start from the first reading?
Starting hottest = 0 fails when every reading is below 0 °C, as in a freezer log: the 'hottest' would wrongly stay 0. Starting from readings_c[0] (or None, then filling it on the first pass) always gives a real reading. Python's max() and min() do the same and should agree with your loop.
Plausibility checks catch glitches
A greenhouse is never −40 °C. A plausibility band (here −10 to 50 °C) flags readings that are physically impossible, usually from a loose wire, electrical noise or a sensor fault. Flag and count them, report them, and leave them out of the mean, rather than silently averaging them in.
Explain it another way
The mean is like the average height of a class: it can't tell you that one person is on stilts and another is sitting on the floor.
Each pass may update several accumulators: counters, running extrema and flags. The extrema must be initialised from the data, not from a constant, to be correct for every input range.
The mean is sensitive to outliers: one value of 85 among six near 22 adds (85 − 22)/7 ≈ 9 °C to it. The minimum and maximum, and a count of out-of-band values, expose what the mean hides.
Industrial data pipelines run plausibility and range checks on every sensor channel before using the data, and raise a maintenance alert when the glitch count rises.
Worked example 1
Freezer log: −19.5, −18.0, −21.2, −18.9 °C. A program starts hottest = 0. What does it report, and how do you fix it?
No reading is above 0, so hottest stays 0 °C: wrong. Start with hottest = readings[0] = −19.5; the loop then finds −18.0 °C, the real maximum.
Common mix-ups
- If the mean is normal, the data is fine.
✓ A mean can hide extreme or impossible readings. Check the max, min and plausibility too. - Glitchy readings should be quietly deleted.
✓ Flag, count and report them. A rising glitch count is itself a warning that a sensor is failing.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3
Why report the max, the min and a glitch count as well as the mean?
Practice 4
Readings 18.2, 25.6, 19.9 and 23.4 °C. What is the range, max − min, in °C?
7.Predict
Readings: 21.4, 21.6, −40.0, 21.9, 22.1, 85.0, 22.0 °C. What is the mean?
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Decisions and loops · Describing data · Noise and filtering