Your report gives totals, but not the expenses behind them. This lesson lists every expense on its own line, without one print per item, so the list can grow and the report keeps up.
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this:
Three amounts on three lines from a single print. That is a for loop. It runs the indented block under it once for every item in the list, in order. Before each pass, Python stores the next item under the name amount. So the block prints 1500, then 2500, then 3500. Done is not indented, so it runs once, after the loop has finished.
The name between for and in is the loop variable. You choose it. A good name says what one item is: one amount from a list of expenses.
A loop's block can hold several lines, and all of them run on every pass:
Two items, two passes, four lines. The block is everything indented under the for line, exactly as with if.
The syntax
for amount in expenses:
print(f" ₦{amount:,}")for, a name of your choice,in, the list, and a colon.- The block is indented by four spaces.
- During each pass, the loop variable holds the current item.
- The loop ends when every item has had its turn. Python then carries on from the first line after the block.
A for loop is the reason a list beats separate variables. Add an expense to the list, and the loop prints it too. You never write another line.
A minimal working example
expenses = [1500, 2500, 3500, 800, 4200]
print("Expenses:")
for amount in expenses:
print(f" ₦{amount:,}")Output: Expenses:, then the five amounts, each with a naira sign. The two spaces at the start of the string indent each amount in the report. They are part of the text, not the code's indentation.
Common mistakes
You print the list instead of the item. Run this:
The whole list printed three times. The block runs once per item, and on each pass it printed expenses, the entire list. Inside the loop, use the loop variable, amount, to work with one item.
You loop over a number. Run this:
TypeError: 'int' object is not iterable"Iterable" means something Python can go through item by item, like a list. total is one number, with no items. Loop over the list itself: for amount in expenses:.
You forget the colon or the indentation. These give the same errors as with if: SyntaxError: expected ':', and IndentationError: expected an indented block after 'for' statement. Every line that opens a block follows the same two rules.
Try it yourself
You will add a list of every expense to the top of your report.
-
Run the starting code below. Confirm your report shows totals but no individual expenses.
-
Find the
print()that makes the blank line under your month. Under it, add these lines:print("Expenses:") for amount in expenses: print(f" ₦{amount:,}") print()Run it. Confirm the report lists every expense on its own line, each starting with
₦, followed by a blank line and thenExpenses recorded. -
Add a sixth expense: put
, 1200before the closing]on yourexpensesline. Run it. Confirm the list in your report gained a line, without you writing anotherprint. -
Remove the
, 1200you added. Run it. Confirm the list is back to its earlier length. -
Break it on purpose: delete the four spaces in front of the
printinside your loop. Run it. Confirm the console shows anIndentationErrorthat mentions the'for'statement. -
Fix it: put the four spaces back. Run it. Confirm your expense list returns.
Your tracker lists every expense, however many there are. The loop only prints, though. The next lesson has it count and add up as it goes, so it can answer questions sum and max cannot.