4.9 Multi-way Decision Statements
A simpleif/elsestatement can select from between two execution paths. Listing 4.11 (enhancedcheckrange.py) showed how to select from among three options. What if exactly one of many actions should be taken?
Nested if/else statements are required, and the form of these nested if/elsestatements is shown in Listing 4.17 (digittoword.py).
Listing 4.17:digittoword.py
value = int(input("Please enter an integer in the range 0...5: ")) if value < 0:
print("Too small") else:
if value == 0:
print("zero") else:
if value == 1:
print("one") else:
if value == 2:
print("two") else:
if value == 3:
print("three") else:
if value == 4:
print("four") else:
if value == 5:
print("five") else:
print("Too large") print("Done")
Observe the following about Listing 4.17 (digittoword.py):
• It prints exactly one of eight messages depending on the user’s input.
• Notice that eachifblock contains a single printing statement and eachelseblock, except the last one, contains anifstatement. The control logic forces the program execution to check each condition in turn. The first condition that matches wins, and its correspondingifbody will be executed. If none of the conditions are true, the program prints the lastelse’sToo largemessage.
As a consequence of the required formatting of Listing 4.17 (digittoword.py), the mass of text drifts to the right as more conditions are checked. Python provides a multi-way conditional construct called if/elif/elsethat permits a more manageable textual structure for programs that must check many con- ditions. Listing 4.18 (restyleddigittoword.py) uses theif/elif/elsestatement to avoid the rightward code drift.
Listing 4.18:restyleddigittoword.py
value = int(input("Please enter an integer in the range 0...5: ")) if value < 0:
4.9. MULTI-WAY DECISION STATEMENTS 94 print("Too small")
elif value == 0:
print("zero") elif value == 1:
print("one") elif value == 2:
print("two") elif value == 3:
print("three") elif value == 4:
print("four") elif value == 5:
print("five") else:
print("Too large") print("Done")
The word elifis a contraction ofelseandif; if you readelifaselse if, you can see how we can transform the code fragment
else:
if value == 2:
print("two")
in Listing 4.17 (digittoword.py) into
elif value == 2:
print("two")
in Listing 4.18 (restyleddigittoword.py).
Theif/elif/elsestatement is valuable for selecting exactly one block of code to execute from several different options. Theifpart of anif/elif/elsestatement is mandatory. Theelsepart is optional. After theifpart and beforeelsepart (if present) you may use as manyelifblocks as necessary.
The general form of anif/elif/elsestatement is
4.9. MULTI-WAY DECISION STATEMENTS 95
condition-1 block-1 if :
elif :
elif :
elif :
else:
default-block block-2
block-3
block-4 condition-2
condition-3
condition-4
. . .
Listing 4.19 (datetransformer.py) uses anif/elif/elsestatement to transform a numeric date in mon- th/day format to an expanded US English form and an international Spanish form; for example,2/14would be converted toFebruary 14and14 febrero.
Listing 4.19:datetransformer.py
month = int(input("Please enter the month as a number (1-12): ")) day = int(input("Please enter the day of the month: "))
# Translate month into English if month == 1:
print("January ", end='') elif month == 2:
print("February ", end='') elif month == 3:
print("March ", end='') elif month == 4:
print("April ", end='') elif month == 5:
4.9. MULTI-WAY DECISION STATEMENTS 96 print("May ", end='')
elif month == 6:
print("June ", end='') elif month == 7:
print("July ", end='') elif month == 8:
print("August ", end='') elif month == 9:
print("September ", end='') elif month == 10:
print("October ", end='') elif month == 11:
print("November ", end='') else:
print("December ", end='')
# Add the day
print(day, 'or', day, end='')
# Translate month into Spanish if month == 1:
print(" de enero") elif month == 2:
print(" de febrero") elif month == 3:
print(" de marzo") elif month == 4:
print(" de abril") elif month == 5:
print(" de mayo") elif month == 6:
print(" de junio") elif month == 7:
print(" de julio") elif month == 8:
print(" de agosto") elif month == 9:
print(" de septiembre") elif month == 10:
print(" de octubre") elif month == 11:
print(" de noviembre") else:
print(" de diciembre")
A sample run of Listing 4.19 (datetransformer.py) is shown here:
Please enter the month as a number (1-12): 5 Please enter the day of the month: 20 May 20 or 20 de mayo
Anif/elif/elsestatement that includes the optionalelsewill execute exactly one of its blocks. The first condition that evaluates to true selects the block to execute. Anif/elif/elsestatement that omits theelseblock may fail to execute the code in any of its blocks if none of its conditions evaluate toTrue.