Debugging For Loops

The list in the program below holds the elevation change in each hour of an eight-hour hike, in meters. A negative change is an hour that went downhill. The program was meant to print the total climb over the uphill hours, the number of downhill hours, and the largest change in any one hour.
changes = [60, 85, -40, 210, -35, -120, 150, 45]
climb = changes[0]
down = 0
steepest = 0
for change in changes:
    if change > 0:
        climb = climb + change
        steepest = change
    if change < 0:
        down = down + change
print(climb, down, steepest)
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

The uphill hours add to 550 meters, three hours went down, and the largest change is 210. The program prints 610 -195 45 instead, and Python reports no error. This lesson traces the loop to find the mistakes and repairs them. Run the program above to see it print 610 -195 45. As the lesson finds each mistake, repair it in the program and run it again.

Tracing the loop

A hand trace follows the loop on paper and records every variable after each pass. The first row below holds the values set before the loop.
Passchangeclimbdownsteepest
before the loop6000
160120060
285205085
3-40205-4085
4210415-40210
5-35415-75210
6-120415-195210
7150565-195150
845610-19545
Three columns go wrong: climb is too high from the first row on, down turns negative at the third pass, and steepest falls at the seventh.

The first pass

The first pass is where the starting values are first used, and the last pass leaves the values the print line reports.
After the first pass, climb should be 60, the first hour's climb, but the table shows 120. The line climb = changes[0] starts climb at 60, and the first pass adds the same 60 again. That bug is before the loop, in a starting value. A sum starts at 0, because nothing has been added before the loop. The starting lines are above the for line, so each runs once.
The line steepest = 0 has the reverse mistake.
A start of 0 does no harm on this hike, since some hour climbed more than 0 meters. It's still wrong, because 0 isn't one of the hours and could be reported as one. The starting lines become
climb = 0
down = 0
steepest = changes[0]
and after the first pass, climb is 60.

The counter

The down column reaches -195 by the sixth pass, and a number of hours can't be negative. The line down = down + change adds each downhill change itself, so down totals the downhill meters as a negative number. That bug is in the body, in the work each pass does.
The repair adds 1 instead, down = down + 1, and down ends at 3, one for each negative change in the list.

The last pass

The last hour climbed 45 meters, less than 210, so steepest should end at 210. With both repairs in place it ends at 45. A running maximum never goes down, yet in a new trace steepest falls from 210 to 150 on the seventh pass. The line steepest = change is inside if change > 0:, so every hour that climbed replaced steepest, larger or not. That bug is in the body too, in which passes do the work. The repair gives that line its own test.
The three if statements can come in any order, since none reads a variable another one changes.
changes = [60, 85, -40, 210, -35, -120, 150, 45]
climb = 0
down = 0
steepest = changes[0]
for change in changes:
if change > 0:
climb = climb + change
if change < 0:
down = down + 1
if change > steepest:
steepest = change
print(climb, down, steepest)
The repaired program prints 550 3 210, the answers worked out at the start.