← Back to course
Code Crew G4 / Week 08 / Tuesday
2/6
Week 08 Β· List or dictionary?

Tuesday

Loop through records
// Records and the right container
⏱ about 15 min

Tuesday: Loop through records

The flight board in the main hall needs to show every launch with its time and name. Rocket writes a print line for launch 0, then launch 1, then launch 2.

"Tomorrow there will be five launches," Raven says. "What do you notice? Every print line is the same except the index."

Rocket groans, then laughs. "A loop! Of course." Nova draws an arrow that slides down the row of cards. "Would you like a hint? Each round, the loop variable holds one whole record."

The loop variable holds a record

A for loop over a list of dictionaries gives you one record each round.

Use a key to read a fact from that record.

launches = [
    {"name": "Moon hop", "gate": 2, "time": "0900"},
    {"name": "Ring tour", "gate": 7, "time": "1300"},
    {"name": "Mars run", "gate": 4, "time": "1600"},
]
for launch in launches:
    print(launch["time"], launch["name"], "at gate", launch["gate"])
The screen shows
0900 Moon hop at gate 2
1300 Ring tour at gate 7
1600 Mars run at gate 4
crew = [
    {"name": "Rocket", "job": "builder", "shifts": 3},
    {"name": "Raven", "job": "explorer", "shifts": 4},
    {"name": "Kai", "job": "pilot", "shifts": 2},
]
total = 0
for person in crew:
    print(person["name"], "is the", person["job"])
    total = total + person["shifts"]
print("Shifts:", total)
PUT THE SCREEN LINES IN ORDER FOR THE PROGRAM ABOVE
  • ?Rocket is the builder
  • ?Shifts: 9
  • ?Raven is the explorer
  • ?Kai is the pilot
WHY THIS EXERCISEA loop over records is how programs make reports and boards.
Kai picks up 3 more shifts, so the record says "shifts": 5. What number shows after Shifts: now?
WHY THIS EXERCISEChange the data, and the same program gives a new answer. No new code needed.
Inside the crew loop aboveTrue or false?
person holds one dictionary each round.?
person["job"] reads the job from that record.?
person[0] gives the name.?
WHY THIS EXERCISEKnowing what the loop variable holds tells you how to read it.
Try it
Read the launch board out loud, one record at a time.
Say the time first, then the name.

Practice lab: count the big pods

pods = [
    {"id": "A", "crew": 3},
    {"id": "B", "crew": 5},
    {"id": "C", "crew": 2},
]
big = 0
for p in pods:
    print("Pod", p["id"], "has", p["crew"])
    if p["crew"] > 2:
        big = big + 1
print("Big pods:", big)
What number ends the first screen line?
How many lines show in all?
What number shows after Big pods:?
LOOP CHECK
  • Read the question.
  • Tap your answer.
In round 2, what does p hold?
How do you read the crew count inside the loop?
Suppose pod C had 4 crew. What would Big pods be?
StatementTrue or false?
The loop runs once for each record.?
p["id"] reads the id of the current record.?
Big pods: prints once for each record.?
A loop can count the records that match a rule.?
WHY THIS EXERCISEIndents decide what repeats, even with records.
A loop adds up the crew in all three pods. What is the total?
WHY THIS EXERCISEA running total works on records. Just read the right key each round.

Nice work! Tomorrow you will pick out only the records you need.

← Monday