← Back to course
Code Crew G5 / Week 12 / Monday
1/6
Week 12 Β· Ship it

Monday

Data and totals
// Build, test and graduate
⏱ about 15 min

Monday: Data and totals

Build week begins! Rocket cracks his knuckles. "I will type the whole tracker before lunch!"

Raven holds up the plan from last week. "What do you notice about the plan?"

"It is in parts," Rocket says. "And there is a test for each part." He takes a breath. "One part at a time."

"I will be navigator, you be driver," Raven says.

Nova hovers between them. "Here is a hint. Every helper needs data to work on. Start there."

Part 1: the data

The supplies go in a dictionary. The command tree from week 10 goes in nested dictionaries.

# Part 1: the data
inventory = {
    "water": 40,
    "seeds": 12,
    "lamps": 3,
    "maps": 7,
    "rope": 2,
    "tools": 15,
}

fleet = {
    "name": "Ada",
    "crew": [
        {"name": "Bo", "crew": [
            {"name": "Cy", "crew": []},
            {"name": "Dee", "crew": []},
        ]},
        {"name": "Eve", "crew": [
            {"name": "Fen", "crew": []},
            {"name": "Gil", "crew": []},
            {"name": "Hal", "crew": []},
        ]},
    ],
}
What is inventory["lamps"]?
How many keys does inventory have?

Part 2: count everything

# Part 2: count everything
def total_items(stock):
    total = ____
    for item in stock:
        total = total + ____
    return total
First blank: total starts at
Second blank: add the count for this item

Test drive

def total_items(stock):
    total = 0
    for item in stock:
        total = total + stock[item]
    return total

print(total_items({"water": 40, "seeds": 12}))
print(total_items({}))
PREDICT THE TEST DRIVE OUTPUT.
  • Read the question.
  • Tap your answer.
What is the first line?
What is the second line?
Using the Part 1 inventory, what would total_items(inventory) return?
WHY THIS EXERCISEtotal_items adds every value in the dictionary.
Driver and navigator
Engineers often build in pairs. The driver types. The navigator reads each line and checks the plan.
Swap roles after each part. Both jobs matter.

Practice lab: more test drives

def total_items(stock):
    total = 0
    for item in stock:
        total = total + stock[item]
    return total

print(total_items({"rope": 2, "maps": 7}))
print(total_items({"tools": 15}))
What does the first print line show?
What does the second print line show?
Using Part 1, what is inventory["water"] + inventory["rope"]?
How many nodes are in the fleet tree from Part 1?
DATA CHECK
  • Read the question.
  • Tap your answer.
Which supply in Part 1 has the biggest count?
Who is the root of the fleet tree?
Why test total_items({}) as well?
StatementTrue or false?
inventory has a key for rope.?
total_items returns the number of keys.?
Eve has three crew in the fleet tree.?
The driver types and the navigator checks.?
WHY THIS EXERCISEKnowing the data and roles well makes the build go smoothly.

Two parts done! Data plus a tested helper is a strong start. Tomorrow we find what is running low.