← Course homeNext: Python return values →Code clears when you leave
MISSION 15 · PYTHON DICTIONARIES

The Parts Pantry

Use a named key to read and update one part count. The other keys keep their own values.

What’s saved: your code, predictions and answers clear when you leave this page or reload. If you turn saving on, only which puzzles you finished and how much help you used is kept on this device. Test accounts also share it, and lesson timing, with the site’s maintainer.

◇ Your mastery goalPython dictionaries · Parts pantry · Set A

Complete all 3 required tasks with a correct prediction, an inspected trace and an explanation that shows the concept. At least 2 must be independent checks, without answer-level help.

✓ Qualifying tasks: 0 / 3◎ Independent checks: 0 / 2 required
PRACTICE · 1 OF 3

Battery shelf

0 / 3 checks

Use three batteries from the shelf, then print the remaining battery count.

1
Read the starting collectionThese authored values are read-only inputs to your program.
inventory'batteries'8'wheels'4'sensors'2

Subtract used = 3 from the batteries value.

2
Predict before RunWhat battery count is read first, and what count will print after the update?
3
Update the dictionary by keyRead and update batteries, then print that value.

Allowed: inventory['batteries'], used, subtraction, and print(...).

Keys identify values by name, even if their display order changes. A missing key stops with a clear error. This is a small bounded Python subset.

4
Collection state · inspected eventInventory values: batteries has 8; wheels has 4; sensors has 2.

Parts dictionary

Starting values (read-only lesson input)

inventory = {"batteries":8,"wheels":4,"sensors":2}

Read a value by its key, not by where the key appears.

Key: "batteries"
8
Key: "wheels"
4
Key: "sensors"
2

Border, ▶ marker and text identify the current read; color is an extra cue.

Help for this dictionary puzzleHints build in steps. Worked help and solutions need a deliberate choice.

Highest help used on this case: none

For Further Reading and Exploration

These are outside sites with their own rules. You will see a notice before you leave. Pick one, not all.

  • Automate the Boring Stuff with Python

    Free book to read online. A chapter on dictionaries covers keys, values and looking things up.

    • Hands-on
    • May need an install
    • Ask an adult
  • CS50's Introduction to Programming with Python

    Free Harvard course with videos and notes. A lecture covers dictionaries, which store pairs of keys and values.

    • Hands-on
    • Advanced
    • May count visits
  • MIT OpenCourseWare 6.100L

    Free college course notes and videos. It moves fast, so take one topic at a time. Look for the lectures on dictionaries.

    • Hands-on
    • Advanced
    • May count visits
  • Raspberry Pi Foundation Projects

    Step-by-step Python projects. Pick one that looks things up, then try a new key. Some need hardware, so check the parts list.

    • Hands-on
    • May count visits