SypherTec
…
PART 2 · 03 OF 083 MIN READUPDATED 20 NOV 2026

elif: More Than Two Choices

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:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Now run this:

THREE PATHS, ONE RUNS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

TWO CONDITIONS ARE TRUE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

TWO SEPARATE IF STATEMENTS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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 elif lines as you need, all at the same indentation as the if.
  • else is optional, and when it is there, it comes last.

A minimal working example

main.py
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:

ELSE IF INSTEAD OF ELIF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
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.

  1. Run the starting code below. Confirm your report ends with a status line.

  2. Under the smallest line, add left = budget - total. Run it. Confirm nothing changed.

  3. Add an elif branch between the if block and the else: line. Keep it at the same indentation as the if.

    elif left < 3000:
        print(f"Status: close to your budget, ₦{left:,} left")

    Change the else block's print to print(f"Status: within budget, ₦{left:,} left"). Run it. Confirm you see one status line. With the amounts given, it is close to your budget, ₦2,500 left.

  4. Test each path. Set your budget to 20000 and Run it: confirm within budget. Set it to 10000 and Run it: confirm over budget. Then put your budget back.

  5. Break it on purpose: change elif to else if. Run it. Confirm the console shows SyntaxError: expected ':'.

  6. Fix it: change it back to elif. Run it. Confirm your status line returns.

TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 2 · 03 OF 08❮❯