Adding up a list

Adding up a list is the most common loop you will ever write: keep a running total, and add each number onto it. The trap is one character — = where you meant += — and the state pane shows exactly what it costs. Try Predict mode and call each total before you reveal it.

Add up all the numbers in the list and report the total.

Adding up a list — the overwrite bug execution-derived · CPython
variables
state after line 1 runs
variablevalue
nums[3,8,5,2]
step 1 / 11

Click a line, drag the slider, or use the keys.

What you are looking at

There is no cup or pile here — just one number, total, that you watch grow (or fail to). Keep your eye on it as each n comes through the loop.

Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.