← Back to course
Code Crew G4 / Week 12 / Friday
5/6
Week 12 Β· Launch day

Friday

Launch day
// Build the launch planner
⏱ about 15 min

Friday: Launch day

The whole station gathers in the control room. Every piece has been built and tested. Rocket is the driver at the keyboard. Raven is the navigator, reading each line and checking the plan.

"What do you notice?" Raven asks the crew, as the final program fills the big screen. "It is all the things we learned, working together."

Nova hovers beside the launch button, her cyan lights pulsing. "Would you like to run it?" she asks. Rocket takes a deep breath. "Let's see what happens!"

The launch planner

# Launch planner
def decode(line):
    parts = line.split(";")
    return {
        "name": parts[0],
        "gate": parts[1],
        "time": parts[2],
        "fuel": int(parts[3]),
        "status": parts[4].upper(),
    }

def total_fuel(launches):
    total = 0
    for launch in launches:
        total = total + launch["fuel"]
    return total

log = [
    "Moon hop;2;0900;30;READY",
    "Mars run;4;1100;70;HOLD",
    "Ring tour;7;1400;25;ready",
    "Comet chase;5;1600;40;READY",
]
budget = 120
ready = []
for line in log:
    launch = decode(line)
    if launch["status"] == "READY":
        ready.append(launch)
for launch in ready:
    print(launch["time"], launch["name"], "gate", launch["gate"])
need = total_fuel(ready)
print("Fuel:", need, "of", budget)
if need <= budget:
    print("Cleared for launch!")
else:
    print("Over budget by", need - budget)
The screen shows
0900 Moon hop gate 2
1400 Ring tour gate 7
1600 Comet chase gate 5
Fuel: 95 of 120
Cleared for launch!
Part of the plannerWhat you learned it in
split, index, upperweeks 5 and 6
dictionary records in a listweeks 7 and 8
for loop, if, append, running totalweeks 3 and 4
def, parameters, returnweek 10
decompose, test, debugweeks 11 and 12
A fifth entry, "Sun watch;1;1800;30;READY", joins the log. What is the new fuel need?
What is the last line then? Type its first word.
DRIVER AND NAVIGATOR
  • Read the question.
  • Tap your answer.
Raven is the navigator. What is her job?
At the computer
This is never required.
If a grown-up has set up Python from python.org, type the launch planner into IDLE.
Take turns as driver and navigator. Then add a launch of your own.

Practice lab: one more piece

def board_line(launch):
    return launch["time"] + " " + launch["name"] + " gate " + launch["gate"]

ready = [
    {"name": "Moon hop", "gate": "2", "time": "0900"},
    {"name": "Ring tour", "gate": "7", "time": "1400"},
]
for launch in ready:
    print(board_line(launch))
print(len(ready), "launches today")
What is the first screen line?
How many lines show in all?
What number starts the last line?
BOARD CHECK
  • Read the question.
  • Tap your answer.
Why are gate and time strings here?
What does board_line return?
Add a third ready launch. How many lines show?
StatementTrue or false?
board_line returns a string.?
board_line prints the line itself.?
The loop calls board_line once for each launch.?
"0900" + " " + "Moon hop" makes "0900 Moon hop".?
WHY THIS EXERCISEThe function builds the line. The loop prints it.
Call board_line({"name": "Sun watch", "gate": "1", "time": "1800"}). Type the first four characters it returns.
WHY THIS EXERCISEThe time comes first, so the board reads in time order.

Liftoff! You read, traced, built and debugged a real Python program.

You are now a Crew Engineer. Be proud of every bug you fixed along the way.

← Thursday