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!"
# 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 planner | What you learned it in |
|---|---|
| split, index, upper | weeks 5 and 6 |
| dictionary records in a list | weeks 7 and 8 |
| for loop, if, append, running total | weeks 3 and 4 |
| def, parameters, return | week 10 |
| decompose, test, debug | weeks 11 and 12 |
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") | Statement | True 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". | ? |
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.