Lesson 042: Exceptions: catching, chaining, guarding
Focus
This page is deliberate repetition with new literals: Intermediate drills Exceptions: catching, chaining, guarding; spin token 373765 makes this page unlike its neighbours.
Key ideas
- Angle
Intermediate: micro cadence2mixesExceptions: catching, chaining, guarding; spin42194. - Ritual: Add one doctest-style assertion comment above hottest print.
- Own one invariant tied to
Intermediate.
Example (LESSON_UID = "intermediate-042")
# Intermediate drill L042 topic-4 micro-1 pattern-10
LESSON_UID = "intermediate-042"
spin_a, spin_b, spin_c = 287, 203, 458
def gate(v):
if v < spin_a + 1:
return "low", v ** 2
if v > spin_b + 42:
return "high", v // max(1, 1 + 1)
return "mid", v + spin_c
cand = [3, 42, 146]
for candidate in cand:
lbl, val = gate(candidate)
print(candidate, lbl, val)
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as scratch:
target = Path(scratch) / "scratch-42.txt"
snap = [55, 347, 639, 931]
target.write_text("\n".join(str(x) for x in snap), encoding="utf-8")
print("scratch_bytes", target.stat().st_size, "rolling", sum(snap) % (458 + 131))
Practice
Practice 33: Shadow one variable purposely to spotlight scope quirks. Literal nudge 33.
Fingerprints
- lesson_uid:
intermediate-042 - umbrella band:
Exceptions: catching, chaining, guarding(2/10) - lesson_index:
1320