← Back to course
Code Crew G4 / Week 07 / Friday
5/6
Week 07 Β· Dictionaries

Friday

Is it on board?
// Look it up by name
⏱ about 15 min

Friday: Is it on board?

Crew members send in requests: tape, rope and water. Rocket's program looks up each one. On rope, Python stops with a KeyError. "We have never had rope!" Rocket says.

Raven reads the error. "What do you notice? The program asked for a key that is not there."

Nova shines a small light into the empty corner of the shelf. "Would you like a hint? You used a word a few weeks ago to check if a string was inside another. It works on dictionaries too."

Check with in first

key in dictionary gives True if the key is there, and False if it is not.

Check with in first. Then you never look up a missing key.

inventory = {"water": 12, "oats": 8, "tape": 3}
requests = ["tape", "rope", "water"]
for r in requests:
    if r in inventory:
        print(r, "on board:", inventory[r])
    else:
        print(r, "not on board")
The screen shows
tape on board: 3
rope not on board
water on board: 12
inventory = {"water": 12, "oats": 8}Does Python say True?
"oats" in inventory?
"rope" in inventory?
12 in inventory?
"Water" in inventory?
WHY THIS EXERCISE12 is a value, not a key, so in says False. That surprises many coders.

Hand out supplies

inventory = {"water": 12, "tape": 1}
requests = ["tape", "tape", "water"]
for r in requests:
    if inventory[r] > 0:
        inventory[r] = inventory[r] - 1
        print("Gave", r)
    else:
        print("Out of", r)
print(inventory)
PUT THE SCREEN LINES IN ORDER FOR THE PROGRAM ABOVE
  • ?Gave tape
  • ?Out of tape
  • ?Gave water
  • ?{'water': 11, 'tape': 0}
WHY THIS EXERCISEA loop that changes a dictionary needs careful tracing, round by round.
SPOT THE RISK
  • Read the question.
  • Tap your answer.
Suppose the requests list above also had "rope". What would happen?
At the computer
This is never required.
If a grown-up has set up Python from python.org, type the requests program into IDLE.
Add your own request to the list.

Practice lab: two checks at once

and is True only when both sides are True. Python checks the left side first, so a missing key is never looked up.

stock = {"tape": 2, "glue": 0}
asks = ["glue", "tape", "rope"]
for a in asks:
    if a in stock and stock[a] > 0:
        print("Yes:", a)
    else:
        print("No:", a)
How many lines start with Yes?
How many lines start with No?
What value does stock["glue"] hold?
IN CHECK
  • Read the question.
  • Tap your answer.
What does "tape" in stock give?
What does 2 in stock give?
Which line can never cause a KeyError?
StatementTrue or false?
in checks the keys, not the values.?
"rope" in stock is True.?
and is True when only one side is True.?
Checking with in first prevents a KeyError.?
WHY THIS EXERCISEand needs both sides True. in keeps lookups safe.
d = {"x": 5}. How many of these give True: "x" in d, 5 in d, "y" in d?
WHY THIS EXERCISEOnly "x" is a key, so only one check is True.

Week seven is done! You can look up, change, add and check keys. Next week, you will choose between lists and dictionaries.

← Thursday