The fuel budget program is ready for the big meeting. Rocket runs it, and it says the trips need only 15 units of fuel. "That is way too low," he says. "We can improve it."
Raven makes a trace table on her notepad. "What do you notice about total each round?" she asks. "It keeps going back to zero," Rocket says.
Nova highlights one line in red. "Let's debug together. Would you like a hint? Look at where total gets its starting value."
This program should show 35. It shows 15. Read it carefully.
trips = [12, 8, 15]
for f in trips:
total = 0
total = total + f
print("Total fuel:", total) | Round | f | total after the round |
|---|---|---|
| 1 | 12 | 12 |
| 2 | 8 | 8 |
| 3 | 15 | 15 |
Rocket wants just one line on the screen. Read this version.
trips = [12, 8, 15]
total = 0
for f in trips:
total = total + f
print("Total fuel:", total) | Change to bug 2 | Does it make just one line show? |
|---|---|
| Take the indent off the print line | ? |
| Indent the total = 0 line | ? |
| Delete the print line | ? |
This program should count the full tanks. Only two tanks are full, but the last line shows 3.
tanks = ["full", "empty", "full"]
count = 0
for t in tanks:
if t == "full":
print("Full tank")
count = count + 1
print(count) | Change to the fixed program | Does the last line still show 2? |
|---|---|
| Move count = 0 inside the loop, above the if | ? |
| Change the list to ["empty", "full", "full"] | ? |
| Count "empty" instead of "full" | ? |
| Add one more "full" to the end of the list | ? |
Week three is done, Crew Engineer! You can loop, total and count. Next week, loops will make choices with if, elif and else.