Your report lists every expense, but the loop only prints them. This lesson has the loop keep count as it goes, and report how many of your expenses were over ₦3,000.
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this, and watch the numbers climb:
running started at 0 before the loop. On every pass, it became its old value plus the current amount: 1500, then 4000, then 7500, and so on. After the last pass, it matched sum exactly.
That is all sum does. The pattern underneath is worth knowing, because it answers questions no built-in function can. A variable that starts before a loop and changes on every pass is called an accumulator.
Here is a question sum cannot answer: how many expenses were over 3,000?
large started at 0. On each pass, the if asked whether the current amount was over 3,000. Only when it was did large go up by one. Two amounts passed the test, so large ended at 2.
Look at the indentation. The if is inside the loop, so it runs on every pass. The line that adds one is inside the if, so it runs only on the passes where the condition is True. The final print is outside both, so it runs once, after the loop.
The syntax
large = 0
for amount in expenses:
if amount > 3000:
large = large + 1
print(large)Three parts, always in this order:
- Start the accumulator before the loop:
0for a count or a total. - Update it inside the loop:
large = large + 1to count,running = running + amountto add up. - Read it after the loop, once every pass has finished.
large = large + 1 reads as "work out large + 1, then store the result back in large". Python has a short form that does the same thing: large += 1. Both are correct. This track writes the long form so each step stays visible.
A minimal working example
expenses = [1500, 2500, 3500, 800, 4200]
large = 0
for amount in expenses:
if amount > 3000:
large = large + 1
print(f"Expenses over ₦3,000: {large}")Output: Expenses over ₦3,000: 2. Change the 3000 in the condition, and the same loop counts against a different limit.
Common mistakes
You start the accumulator inside the loop. Run this:
It printed 1, not 2. large = 0 ran at the start of every pass, so each pass wiped out the count before it. Only the last pass survived, and 4200 is over 3,000, so the answer was 1. Start the accumulator once, before the loop.
You never start it at all. Run this:
NameError: name 'large' is not defined. Did you mean: 'range'?On the first matching pass, large = large + 1 needs the current value of large, and there is none yet. Python's suggestion is a guess based on spelling, and here it is wrong. Add large = 0 above the loop.
You work out the new value but never store it. large + 1 on a line of its own gives no error, and changes nothing. The count stays at 0. Write large = large + 1, or large += 1.
Try it yourself
You will count your large expenses inside the loop that lists them.
-
Run the starting code below. Confirm your report lists your expenses.
-
Directly above your
forline, addlarge_spends = 0, at the same indentation as thefor. -
Inside the loop, under the
print, add anifthat counts amounts over 3,000:if amount > 3000: large_spends = large_spends + 1The
ifis indented four spaces, to sit inside the loop. The line under it is indented eight. -
Under your
Smallest spendprint, addprint(f"Expenses over ₦3,000: {large_spends}"). Run it. Confirm the new line matches the number of amounts over 3,000 in your list. With the amounts given, it is2. -
Break it on purpose: move
large_spends = 0inside the loop, as its first line, indented four spaces. Run it. Confirm the count drops: it now shows1if your last expense is over 3,000, and0if not. -
Fix it: move
large_spends = 0back above theforline, with no indentation. Run it. Confirm your count is correct again.
Your loop now counts as well as prints. Every loop so far has run once per item in a list. The next lesson meets a loop that runs until something changes, however many passes that takes.