AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Ladders and mixed-up lines

Week 7 · 1.4 Conditional statements in text-based programming · 3 lessons
2

Several elifs, one else

Computing 8: Topic 1.4, pp. 70–105
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 = 14
4elif member == "adult":
5    days = 21
6elif member == "senior":
7    days = 28
8else:
9    days = 7
10print("Loan days:", days)
Output
Loan days: 14
Worked examples
Member typedLoan days printed
child14
adult21
senior28
guest7
Adult7
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 = 14
4elif member == "adult":
5    days = 21
6elif member == "senior":
7    days = 28
8print("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.
VersionOutput for 15
two separate if statementsBig then Medium
if ... elifBig
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?
Python
1colour = input("Colour? ")
2if colour == "red":
3    print("Stop")
4elif colour == "amber":
5    print("Get ready")
6elif colour == "green":
7    print("Go")
8else:
9    print("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.
3
This program stops with an error when the user types green. Name the error, explain what causes it and fix the program.
Python
1shape = input("Shape? ")
2if shape == "square":
3    points = 4
4elif shape == "triangle":
5    points = 3
6print("Corners:", points)
4
Explain in your own words why elif is better than separate if statements when only one answer should be printed.