Inventory Lookup
beginnerUp next
Up next
Output will appear here...
The starter code builds a dictionary called inventory from the first lines of input (item name and quantity), then reads one more line, query, which is the name of an item to look up.
Print the quantity in stock for that item. If the item isn't in the inventory, print not in stock. An item with quantity 0 is in the inventory, so print 0 for it.
For this input:
3
apple 5
pear 0
fig 12
fig
a correct solution prints 12. Names are case-sensitive, so Plum is not the same item as plum. If an item is listed twice, the later quantity replaces the earlier one. The starter code already handles that.
Tip
inventory.get(query, ...) can do the lookup and the fallback in one step. Be careful: a quantity of 0 is falsy, so avoid if inventory.get(query): as your check.
No comments yet. Start the discussion.