Find a Reset in the Wrong Place
Move initialization outside a loop and retest its total.
Supplied materials
- Paper and pencil; an adult may read aloud. No device, account or purchase is needed.
Learn the idea
Where we set a starting value matters. Setting a variable to zero before a loop lets it collect values across repetitions. Setting it to zero inside the loop discards the earlier total each time. Debug by tracing the supplied statements exactly, then deciding where initialization belongs.
Goal: sum [2, 3, 4]. Faulty draft: for each item, set total = 0, then set total = total + item; after the loop, show total. Every iteration starts over, so only the last item survives. Repair: set total = 0 once before for each item, then keep only the addition inside.
- Trace the reset and addition as two actions for each item.
- Record final values 2, 3 and 4 after successive iterations of the draft.
- Move total = 0 before the loop.
- Retest the same list and an empty list; the repaired empty-list total stays 0.
See the method in action
Faulty trace: 0 → 2, reset 0 → 3, reset 0 → 4; shown 4. Repaired trace: 0 → 2 → 5 → 9; shown 9. An empty list visits no items, so the repaired result is 0.
Check your reasoning
Your answer is temporary and is not saved to an account. Download your current answer before leaving or reloading. Written work on paper or in another app stays in your own file.
See the answer and explanation
2
Each reset discards the previous sum; the final visit resets to 0 and adds 2. Moving the reset before the loop would instead give 9.
Take it further
Change the repaired list to [5, 0, 2]. Expected trace: 0 → 5 → 5 → 7. The zero item still gets visited but does not change the total.
Nothing here is saved to an account or sent to a teacher. The answer download includes your current choice and any checked feedback.
Check the subject sources
These LifeStarter passages, scenarios and exercises are authored for this Academy with AI assistance and review. External resources keep their publishers’ terms. A grade label suggests a starting level; it does not establish alignment with your school’s complete requirements.