try/except — catching what AI didn't — step 9 of 9
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
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
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.