Home » Chapter 3 : Decisions
if-elif-else Statements
So far we have learned two decisions structures, the if and the if-else. Now we'll consider the if-elif-else which allows us to chain numerous decision conditions into one decision structure. In programming, we often need to decide from more than just two possible options. For example, if we need to write a program that determines students' letter grades based on their scores, a True/False decision structure (if-else) will not suffice. This is where the if-elif-else structure comes in.
if condition_1:
code_block_based_on_condition_1
elif condition_2:
code_block_based_on_condition_2
elif condition_3:
code_block_based_on_condition_3
.
.
.
elif condition_n:
code_block_based_on_condition_n
else
code_block_based_on_else
Notice the indentation. The lines of code in the code blocks for the if and all subsequent elif's are indented so the interpreter knows what lines of code are included in those blocks.
message = "The month's name is "
print("Program Started.")
month_number = int(input("Please enter the month number (1 to 12): "))
if month_number == 1:
message = message + "January"
elif month_number == 2:
message = message + "February"
elif month_number == 3:
message = message + "March"
elif month_number == 4:
message = message + "April"
elif month_number == 5:
message = message + "May"
elif month_number == 6:
message = message + "June"
elif month_number == 7:
message = message + "July"
elif month_number == 8:
message = message + "August"
elif month_number == 9:
message = message + "September"
elif month_number == 10:
message = message + "October"
elif month_number == 11:
message = message + "November"
elif month_number == 12:
message = message + "December"
else:
message = "Invalid Month Number"
print(message)
print("Program completed.")
Program Started.
Please enter the month number (1 to 12): 5
The month name is May
Program completed.
Program Started.
Please enter the month number (1 to 12): 55
Invalid Month Number
Program completed.
For these problems, you may need to refer to the Comparison Operators and Logical Operators tables on the previous if statement page. You may also need the table operators on the Basic Operators page as well. These tables are also available in the Reference Materials.
if-elif-else Statement: Write a Python program that prompts the user for an integer and then prints whether that number is positive, negative, or zero.
Please enter an integer: -5
Your integer is negative.
if-elif-else Statement: Write a Python program that prompts the user for two integers and then prints their relationship, that is, is the first number larger than the second, equal to the second, or smaller than the second?
--------------------------------------------------
This program will prompt you for two integers,
then it will tell you their relationship:
--------------------------------------------------
Please enter your first integer: 10
Please enter your second integer: 20
Your first integer is smaller than your second integer.