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

Friday

The fuel budget
// Records and the right container
⏱ about 15 min

Friday: The fuel budget

The station has 150 units of fuel for this week. Four trips are planned, and each one needs a different amount. Rocket wants to know two things: do the trips fit the budget, and which trip is the hungriest?

"I will add them on my tablet," he says. Raven shakes her head. "What do you notice? We already know how to total a list and find a champion."

Nova glows. "Would you like a hint? Put the patterns together. They work on records too."

Total, champion and a choice

trips = [
    {"name": "Moon hop", "fuel": 30},
    {"name": "Mars run", "fuel": 70},
    {"name": "Ring tour", "fuel": 25},
    {"name": "Comet chase", "fuel": 40},
]
budget = 150
total = 0
hungriest = trips[0]
for t in trips:
    total = total + t["fuel"]
    if t["fuel"] > hungriest["fuel"]:
        hungriest = t
print("Total:", total)
print("Hungriest:", hungriest["name"])
if total <= budget:
    print("Within budget")
else:
    print("Over by", total - budget)
The screen shows
Total: 165
Hungriest: Mars run
Over by 15

The champion here is a whole record, not just a number. That way the program can show its name.

The trips need 165 units, but the budget is 150. So the plan is 15 units over.

The crew drops the Ring tour. What is the new total?
What does the last line say then?
CHAMPION RECORD
  • Read the question.
  • Tap your answer.
Why does hungriest store the whole record t, not just t["fuel"]?
Suppose Comet chase needs 90 units instead. What name shows after Hungriest:?
WHY THIS EXERCISETesting what if questions by hand is how you check a program's logic.
At the computer
This is never required.
If a grown-up has set up Python from python.org, type the fuel budget into IDLE.
Change the numbers and see what happens.

Practice lab: a smaller budget

trips = [
    {"name": "Probe", "fuel": 20},
    {"name": "Lander", "fuel": 45},
    {"name": "Scout", "fuel": 15},
]
budget = 90
total = 0
for t in trips:
    total = total + t["fuel"]
left = budget - total
print("Total:", total)
print("Left:", left)
What number shows after Total:?
What number shows after Left:?
If the budget were 100, what would Left be?
BUDGET CHECK
  • Read the question.
  • Tap your answer.
Which trip is the hungriest?
Which if could find the trip that needs the least fuel?
Is the plan within the budget of 90?
StatementTrue or false?
total starts at 0 before the loop.?
left is worked out inside the loop.?
t["fuel"] is a number, so it can be added.?
Adding a fourth trip needs a new loop.?
WHY THIS EXERCISEThe same loop works for any number of records.
A fourth trip, {"name": "Relay", "fuel": 10}, is added to the list. What is the new total?
WHY THIS EXERCISEThe loop picks up the new record with no other changes.

Week eight is done, Crew Engineer! You can store records and choose containers. Next week, your programs will ask the user questions.

← Thursday