An if and an else give a program two branches: one for when the condition is
true, one for everything else. Plenty of decisions have more than two possible
answers, though. A weather app does not only know rain and not-rain, and a
guessing game does not only know right and not-right.
For a third outcome, and a fourth, and a tenth, Python has elif.
elif goes between the if and the else, and it carries a condition of
its own. You can add as many elif branches as you need.
Here is a weather program with three outcomes. The elif in the middle is the
new part:
weather = "snow"
if weather == "rain":
print("Take your umbrella")
elif weather == "snow":
print("Take your scarf")
else:
print("No special advice for you")
Take your scarf
weather does not hold "rain", so the if branch is skipped. Python moves on
to the elif and checks its condition. weather does hold "snow", so that
branch runs and the else is never reached.
Four outcomes need two elif branches. Nothing else about the program changes:
weather = "sunny"
if weather == "rain":
print("Take your umbrella")
elif weather == "snow":
print("Take your scarf")
elif weather == "sunny":
print("Take your sunglasses")
else:
print("No special advice for you")
Take your sunglasses
Two conditions are false this time before a true one is found, so the program has to look at three branches to output one line.
The rules are short, and every program in this chapter follows them:
if.elif condition is only checked when every condition above it was false.else comes last and has no condition. It runs only when every
condition above it was false.Point 3 is the one that catches people out. A number can satisfy two conditions at once, but only the higher one in the chain ever gets to act on it. Ordering your conditions is part of writing the program.
if, and set the first condition.elif branches as you need, and give each one a new condition.else, which needs no condition.The number of elif branches you need is two less than the number of outcomes:
one outcome is covered by the if, and one by the else.
This program compares two numbers the user types in, and it has three outcomes: the first number is bigger, the second number is bigger, or they are equal.
print("Enter a number:")
num1 = int(input())
print("Enter another number:")
num2 = int(input())
if num1 > num2:
print(str(num1) + " is bigger")
elif num1 < num2:
print(str(num2) + " is bigger")
else:
print("The numbers are the same")
Seven things to check every time you write a chain like this one:
if.elif, and write a condition for your second outcome.elif branches with conditions until only one outcome is left.else for that final outcome. No condition needed.The if, the elif and the else all start at the left hand margin. Only the
code inside a branch is indented, and that indentation is what tells Python
which branch a line belongs to.
If you draw the chain out, it is a row of decisions rather than a single one, shown here as a flowchart.
Reading it from the top:
if condition. If it is true, the code indented
under the if runs, and the program is done deciding.elif condition. If that one
is true, the code indented under that elif runs, and the program is done.elif condition, and so
on down the chain. There can be as many elif conditions as you need between
the if and the else.else, and run the code indented under it.Every route through that diagram ends in exactly one branch. However long the chain gets, a selection statement runs one branch and one branch only.
Run the code below and type rain, then run it again with snow, sunny and
something that is none of the three. Then add a fourth kind of weather with
another elif, and check that your new branch runs.
print("What is the weather like?")
weather = input()
if weather == "rain":
print("Take your umbrella")
elif weather == "snow":
print("Take your scarf")
elif weather == "sunny":
print("Take your sunglasses")
else:
print("No special advice for you")
Make sure that you check for the following things:
if, has as many elif branches in the middle as you
need, and ends with else.if, elif and else line.if, elif and else lines themselves are not indented.if and each elif.else.==) in a condition that checks whether
two pieces of data are the same. A single = stores data in a variable.