Skip to content
Lesson 6 of 13

Step 1 of 8 · Reading · ~4 min

Learn

Making Decisions

If, Elif, Else

A comparison hands you a True or a False. An if statement turns that answer into different behaviour. By the end of this lesson you can write a chain that picks exactly one outcome out of many, and spot the ordering mistakes that silently pick the wrong one.

if: a block that may or may not run

python

Four parts, all required: the keyword if, a condition that is True or False, a colon ending that line, and an indented block underneath.

Python evaluates the condition once. 34 > 30 is True, so the block runs and prints heat warning. Set the first line to temperature = 12 instead and the condition is False, the block is skipped, and the program prints nothing. A skipped block is not an error; it is the normal outcome about half the time.

Indentation is the syntax

Other languages mark a block with braces. Python uses leading spaces, and they are syntax:

python

2 > 5 is False, so both indented lines are skipped and the only output is the last one. Use 4 spaces per level, and never mix tabs with spaces: Python answers that with TabError.

else: the other path

python

else takes no condition of its own: it runs precisely when the if condition was False. Exactly one of the two blocks runs, never both and never neither.

elif: more than two outcomes

elif is short for "else if": a condition consulted only when every test above it failed. A parcel price with four weight bands:

python

Trace it with weight at 7. 7 > 20 is False, move down. 7 > 10 is False, move down. 7 > 2 is True, so cost becomes 18, the chain ends there, the else is never reached, and the output is 18. A chain is read top to bottom and stops at the first True condition. Note too that a branch body is ordinary code: these branches assign rather than print.

Order decides the answer

7 > 2 is true for a 25 kg parcel too. The only reason such a parcel is not billed 18 is that the > 20 test sits above it. Put the loosest test first and everything below is unreachable:

python

That prints 18, where the correct chain prints 45 for the same parcel. No error and no warning, just a wrong number. Order conditions from the most demanding down to the least.

The edge value belongs to one side only

> and >= disagree about exactly one input, and that input is the one a test suite aims at:

python

That prints on time. Swap the condition to minutes >= 30 and the same run prints late. Neither operator is right on its own: decide in words first ("30 minutes counts as late"), then pick the one matching that sentence.

When nothing matches

A chain with no final else is legal. If no condition holds, no block runs:

python

That prints nothing at all. When every input must produce output, end the chain with else rather than one more condition.

Common mistakes

  • Separate if statements instead of one chain. Each if is tested independently, so a value satisfying three of them runs three blocks and prints three lines where one was wanted.
  • Loosest condition first. Every input matches the top branch, so every input gets the same answer and the branches below never run.
  • > where >= was meant. Only the exact boundary input is wrong; everything else looks right, which is why this bug survives casual testing.
  • No final else. An input below every condition prints nothing: empty output, no error.

Your exercise

Turn a numeric score into one letter with a single chain. The starter already reads the number into score; the decision is yours. Scores landing exactly on a band edge are what this exercise catches, so settle which side of each edge they belong to before choosing between > and >=.

Up nextCombining ConditionsMaking Decisions

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…