GoalUse several elif parts with text values, explain why the final else is optional but useful, and compare elif with separate if statements.
New words
optional · allowed but not requiredNameError · the message Python gives when a variable name has not been given a valuecatch-all · a branch (the final else) that handles every value the other conditions did not
Explanation
A chain can have as many elif parts as you need, and they can compare text as well as numbers. Each text value must match exactly, including capital letters. The final else is optional, but it is a good habit: it is the catch-all for everything else, such as a type of member nobody expected. Without it, a variable that was set only inside the chain might never get a value. Then a later line that uses the variable stops the program with a NameError, which is a runtime error. Remember also that elif is not the same as several if statements. With separate ifs Python tests every condition, so more than one message can appear.
Python
1member = input("Member type? ")
2if member == "child":
3 days = 144elif member == "adult":
5 days = 216elif member == "senior":
7 days = 288else:
9 days = 710print("Loan days:", days)
Output
Loan days: 14
Worked examples
Member typed
Loan days printed
child
14
adult
21
senior
28
guest
7
Adult
7
The library loan chain. The typed text is compared with each word in turn. A word that is not child, adult or senior is caught by the else. A capital letter makes a different word, so Adult is not adult.
Python
1member = input("Member type? ")
2if member == "child":
3 days = 144elif member == "adult":
5 days = 216elif member == "senior":
7 days = 288print("Loan days:", days)
Output
NameError: name 'days' is not defined
Take away the else. A guest is not child, adult or senior, so days is never given a value. The last line then stops the program. This is a runtime error, because the program began to run. Python shows this as its last line when guest is typed.
Version
Output for 15
two separate if statements
Big then Medium
if ... elif
Big
Separate ifs and an elif chain give different output for 15. The separate ifs test both conditions, so two lines are printed. The chain stops after the first true condition and prints one line.
Class activity
Sort the members (no computer). Make a stack of ten index cards and write one member word on each, such as child, adult, senior, Child, guest and student. Lay out four labelled boxes: child, adult, senior and else. Take the cards one at a time and put each in the first box it matches, comparing exactly. Which cards ended up in the else box? Why did Child go there? Then add a fifth box for a new type of member and write the extra elif for it. If you have a computer or tablet, ask an adult to run the loan program with some of your words.
Practice
1
Follow the program with red, green, Amber and blue. What is printed each time?
red: Stop. green: Go. Amber: Not a light colour, because the capital A makes it different from amber. blue: Not a light colour.
2
Write a Python program that asks for a ticket type. For child print 3, for adult print 8, and for any other type print Ask at the desk.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): ticket = input("Ticket type? ") / if ticket == "child": / [indent] print(3) / elif ticket == "adult": / [indent] print(8) / else: / [indent] print("Ask at the desk")
3
This program stops with an error when the user types green. Name the error, explain what causes it and fix the program.
It is a NameError, a runtime error. For green no branch runs, so points is never given a value and the last line cannot use it. Fix: add an else branch that sets points (for example points = 0).
4
Explain in your own words why elif is better than separate if statements when only one answer should be printed.
With elif, Python stops testing after the first true condition, so only one branch runs. Separate ifs are all tested, so several messages could be printed for one input.