Dictionaries & Data — Lesson 4: Calculate an inventory order
New skill: Dictionary items, nested lookup, accumulators
Previously learned: Units 01–08
Why this matters
Inventory and checkout tools combine multiple data sources while handling missing information carefully.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
quantities = {'pen': 3, 'notebook': 2}
prices = {'pen': 2, 'notebook': 5}
total = 0
for item, count in quantities.items():
total += count * prices.get(item, 0)
print(total)
Step 2 — Trace the code
- Read
.items()as pairs of an item name and its quantity. - Find the price for each name with a default of zero.
- Multiply quantity by price and add the subtotal to a running total.
Step 3 — Check your understanding
What is the total for two pens at 3 each and one binder at 7?
Step 4 — Watch for this mistake
Do not add quantities directly to prices. Multiply each item’s quantity by its price.
Practice: Calculate an inventory order
Complete order_total(quantities, prices) to return the total price. Missing prices count as zero.
Function contract: order_total(quantities, prices)
Starter file: main.py
Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.
Hint 1 — Start
Start a numeric total at zero.Hint 2 — Build
Loop over `quantities.items()`.Hint 3 — Test
Multiply each count by `prices.get(item, 0)` before adding it.Reflect
In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.
Optional reading
For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.