The crew wants the report to cover tape too. Rocket copies the fuel chunk, pastes it at the bottom, and changes the names. When he runs it, Tape shows 125. That is the fuel number!
"What do you notice?" Raven asks, pointing at one line. Rocket groans. "I missed a name when I changed the copy."
Nova blinks thoughtfully. "Interesting! Would you like a hint? When code is copied again and again, every copy is a new place for a bug to hide."
The three chunks in the station report are almost the same. Only the names change.
Copying a chunk is fast, but it is easy to miss one name. Here is Rocket's tape chunk.
fuel = [40, 35, 50]
tape = [3, 1, 2]
fuel_total = 0
for n in fuel:
fuel_total = fuel_total + n
tape_total = 0
for n in tape:
tape_total = tape_total + n
print("Tape:", fuel_total) | The screen shows |
|---|
| Tape: 125 |
| Part of each chunk | Same in every chunk? |
|---|---|
| total = 0, then a loop that adds | yes |
| the list being added | no: water, oats or fuel |
| the label printed | no: Water, Oats or Fuel |
| the check: is it under 20? | yes |
| Statement | True or false? |
|---|---|
| The three chunks do the same job with different data. | ? |
| Each chunk needs a different loop. | ? |
| The parts that change could become parameters. | ? |
| Copying chunks makes bugs less likely. | ? |
shuttle = [5, 7]
rover = [2, 4]
shuttle_total = 0
for n in shuttle:
shuttle_total = shuttle_total + n
rover_total = 0
for n in shuttle:
rover_total = rover_total + n
print("Shuttle:", shuttle_total)
print("Rover:", rover_total) | Statement | True or false? |
|---|---|
| Both chunks have the same shape. | ? |
| The bug is a missed name in a copied chunk. | ? |
| A function would need a new loop for each vehicle. | ? |
| Fixing one copy fixes all the copies. | ? |
Sharp eyes! Tomorrow you will turn the repeated chunk into a function.