Your tracker has two answers: over budget or within. A total ₦100 under budget deserves a warning that a total ₦10,000 under does not. This lesson adds a third status in between.
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this:
The total is under budget, but only ₦2,500 under. The middle line caught it: elif left < 3000:. elif is short for "else if". It adds another condition between the if and the else.
Python checks the conditions from the top. It runs the block of the first condition that is True, then skips everything else in the chain. If none are True, the else block runs.
"The first True wins" has a consequence. Run this:
4200 is over 3,000, but the program said over 1,000. Both conditions are True, and the first one won. Order the conditions from the strictest to the loosest, so the narrow case gets a chance before the wide one catches everything.
Compare that with two separate if statements:
Both lines printed. Separate if statements are separate questions, and each one runs whenever its own condition is True. Use elif when only one answer should apply.
The syntax
if total > budget:
print("Status: over budget")
elif left < 3000:
print("Status: close to your budget")
else:
print("Status: within budget")elif, a condition, and a colon. Its block is indented like any other.- You can have as many
eliflines as you need, all at the same indentation as theif. elseis optional, and when it is there, it comes last.
A minimal working example
total = 12500
budget = 15000
left = budget - total
if total > budget:
print("Status: over budget")
elif left < 3000:
print(f"Status: close to your budget, ₦{left:,} left")
else:
print(f"Status: within budget, ₦{left:,} left")Output: Status: close to your budget, ₦2,500 left. Change budget to 20000 and the else block runs instead.
Common mistakes
You write else if. Run this:
SyntaxError: expected ':'else never takes a condition, so Python expects a colon straight after it. The word for a second condition is elif, as one word.
Your conditions are in the wrong order. The 4200 example above shows the symptom: no error, but the wrong message. When a later branch never seems to run, check whether an earlier, looser condition is catching its cases first.
You put elif after else. else must be the last branch. An elif after it gives SyntaxError: invalid syntax. Move the elif above the else.
Try it yourself
You will give your tracker a third status, and show how much is left to spend.
-
Run the starting code below. Confirm your report ends with a status line.
-
Under the
smallestline, addleft = budget - total. Run it. Confirm nothing changed. -
Add an
elifbranch between theifblock and theelse:line. Keep it at the same indentation as theif.elif left < 3000: print(f"Status: close to your budget, ₦{left:,} left")Change the
elseblock's print toprint(f"Status: within budget, ₦{left:,} left"). Run it. Confirm you see one status line. With the amounts given, it isclose to your budget, ₦2,500 left. -
Test each path. Set your budget to
20000and Run it: confirmwithin budget. Set it to10000and Run it: confirmover budget. Then put your budget back. -
Break it on purpose: change
eliftoelse if. Run it. Confirm the console showsSyntaxError: expected ':'. -
Fix it: change it back to
elif. Run it. Confirm your status line returns.
Your tracker now chooses between three messages. It still describes your expenses only as totals, though. The next lesson goes through the list one expense at a time and prints each one.