← Back to course
Code Crew G4 / Week 12 / Thursday
4/6
Week 12 Β· Launch day

Thursday

Fix the comms-log bugs
// Build the launch planner
⏱ about 15 min

Thursday: Fix the comms-log bugs

Rocket runs an early version of the planner. It says Moon hop needs 900 units of fuel! "That is impossible," he says. "The whole station only has 120."

Then a second problem appears. The Ring tour is missing from the board, even though the log says it is ready. Raven gets out her notepad. "What do you notice? Two bugs, two clues."

Nova highlights the decode function. "Let's debug together. Would you like a hint? Check which piece goes with which key."

Bug 1: swapped indexes

def decode(line):
    parts = line.split(";")
    return {"name": parts[0], "time": parts[3], "fuel": int(parts[2])}

launch = decode("Moon hop;2;0900;30;READY")
print(launch["name"], "needs", launch["fuel"])
print("Time:", launch["time"])
The screen shows
Moon hop needs 900
Time: 30
Index01234
PieceMoon hop2090030READY

int("0900") is 900. The fuel and the time were read from the wrong pieces.

Fuel should read parts[ ? ]. Type the number.
Time should read parts[ ? ]. Type the number.

Bug 2: the missing launch

log = ["Ring tour;7;1400;25;ready", "Comet chase;5;1600;40;READY"]
count = 0
for line in log:
    parts = line.split(";")
    status = parts[4]
    if status == "READY":
        count = count + 1
print(count, "ready")
The screen shows
1 ready
FIX BUG 2
  • Read the question.
  • Tap your answer.
Why is the Ring tour not counted?
Which line fixes it?
After the fix, how many does the program count as ready?
WHY THIS EXERCISEReal data is messy. Good programs clean it up before they check it.
Try it
Write a log entry with a mistake in it, like a lowercase status.
Ask a grown-up to find your bug.

Bug 3: fuel for every launch

log = ["Moon hop;2;0900;30;READY", "Mars run;4;1100;70;HOLD"]
total = 0
for line in log:
    parts = line.split(";")
    total = total + int(parts[3])
print("Ready fuel:", total)
What number does the program show?
What number should it show for ready launches only?
Which launch should be skipped?
FIX BUG 3
  • Read the question.
  • Tap your answer.
What is missing?
Which line goes above the adding line, inside the loop?
After the fix, what does the adding line need?
Change to the fixed programDoes it still show 30?
Change Mars run to READY?
Add "Sun watch;1;1800;30;HOLD" to the log?
Change the Moon hop fuel to 35?
Rename total to need on every line?
WHY THIS EXERCISETrying changes in your head is how you test a fix.
In the fixed program, add "Sun watch;1;1800;30;ready" to the log. What number shows?
WHY THIS EXERCISEMoon hop and Sun watch are both ready, so 30 plus 30.

Both bugs are squashed! Tomorrow is launch day. You will run the whole planner.

← Wednesday