promptdojo_

raise — failing loudly on purpose — step 7 of 8

Checkpoint

One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.

Last drill. Write a tiny order system:

  1. Define a function place_order(inventory, item, qty) that:
    • Raises KeyError(item) if item is not in inventory.
    • Raises ValueError(f"only {inventory[item]} left of {item}") if inventory[item] < qty.
    • Otherwise returns the string f"shipped {qty} {item}".
  2. Loop over three orders. For each, wrap the call in try/except (KeyError, ValueError) as err: print(err).

Use this inventory and these orders:

inventory = {"book": 5, "pen": 0}
orders = [("book", 2), ("pen", 1), ("hat", 1)]

Expected output:

shipped 2 book
only 0 left of pen
'hat'

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