error handling — when ai's code crashes mid-flight9 / 9
try/except — catching what ai didn't
Checkpoint
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
One last drill. AI wrote a function that looks up a price in a catalog dict
and converts a quantity string to an integer. Either lookup can fail. Wrap
the body in a try/except that catches both KeyError and ValueError,
and returns the string "invalid" for either case.
Two except blocks are fine — or one block with a tuple. Either works.
Expected output when you run:
20
invalid
invalid
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _