Keep a running total — quick review
Picture a cashier starting a sale at 0.
A running total or a count is set to its starting value before the loop and changed inside the body, so each pass builds on the last. The biggest value so far starts with the first value and is replaced inside the loop when an if finds a bigger one. x += 1 adds 1 to x.
Decide what to keep: a total, a count or the biggest value.
Before the loop, give it a starting value: 0 for a total or a count, the first value for the biggest. A line such as
total = total + stepsreads the name before it changes it, so the name needs a value first.Inside the body, change it on the passes that need it: add each value to a total, add 1 when a pass matches, or replace the biggest when an
iffinds a bigger value.After the loop, use the result: display it, or, when the count is above 0, divide the total by the count for an average.
Trace two passes by hand and check that the value after each pass builds on the one before.
A total set to 0 at the top of the body starts again on every pass. With total = 0 as the body's first line, the step program would display Total: 4000, the last day's steps alone, and Average: 1000.0, that one day divided by 4. Put the starting value above the for line.
Starting the biggest value at 0 goes wrong when every value is below 0: no value is bigger than 0, so the program would display 0, a number that was never read. Start with the first value instead, as the worked example does.