
Predict
Side Quest: Missing, Zero or Blank?
A fuel log can say "no reading" in several ways:
| Record | What it is |
|---|---|
{} |
the key is absent |
{"fuel": None} |
the key is there, but the value is None |
{"fuel": " "} |
blank text |
{"fuel": 0} |
a real reading: the tank is empty |
The Guild's policy: absent, None and blank text all mean
"unknown". But 0 is a real measurement, and it must be kept.
A popular shortcut, value or default, swaps in the default for
every falsy value. Read both functions carefully.
def reading(rec):
value = rec.get("fuel")
if value is None:
return "unknown"
if isinstance(value, str) and not value.strip():
return "unknown"
return value
def quick(rec):
return rec.get("fuel") or "unknown"
logs = [{"fuel": 0}, {"fuel": None}, {}, {"fuel": " "}]
print([reading(r) for r in logs])
print([quick(r) for r in logs])