promptdojo_

try/except — catching what AI didn't — step 9 of 9

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

full-screen editor opens — close anytime to keep reading.