Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
What should a parser return when a row cannot be trusted?
Show the answer
A clear failure value such as None, or a structured error.
Presence is one validation rule, not a full schema
all(key in record for key in required) generates a membership test for every required key and returns True only if every test succeeds. The generator supplies one boolean at a time. This checks key presence only: empty or wrongly typed values can still pass.
and short-circuits. Put the presence check first so a missing score stops evaluation before a direct score lookup. The range demonstration assumes numeric scores and accepts inclusive 1–5, including decimals. Label examples assume string labels or an absent key. The later independent audit has a stricter typed policy.
# Presence is one validation rule, not a full schema.
def in_range(record):
required = ["item_id", "label", "score"]
present = all(key in record for key in required)
return present and 1 <= record["score"] <= 5
print(in_range({"item_id": "Museum", "label": "pass"}))
print(in_range({"item_id": "Canal",
"label": "pass", "score": 5}))
False, then True. The first record does not crash because and never evaluates its missing score.
State what a validator proves and what it assumes. A field existing is not evidence that its data is usable.
Predict, explain, then test
These three brief checks are self-reviewed, not a scored quiz. Answer before opening the comparison. Coding checks follow the stated task assumptions on unfamiliar inputs.
Limit the claim
Can a presence-only validator accept a score of None?
Compare your answer · self-reviewed
Yes, if the score key exists. Presence alone says nothing about type or range.
Explain safety
Why put required-field checks before a direct score lookup joined by and?
Compare your answer · self-reviewed
Short-circuit evaluation stops after False, preventing a missing-score lookup.
Trace the boundary
Which satisfies the demonstration range: 1, 5 or 6?
Compare your answer · self-reviewed
1 and 5 pass the inclusive bounds; 6 does not. This numeric demonstration is not yet an integer-only schema.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.