if/elif/else Statement¶
The if statement can also be extended with additional conditions by using
elif. It is possible to add more then one elif statement.
A elif code block is only executed if its boolean expression evaluates to
True and all previous if and elif statements evaluated to False.
Additionally the else code block is only executed if all if and elif
statements evaluated to False.
if condition:
CODE_BLOCK
elif condition_2
CODE_BLOCK_2
elif condition_x
CODE_BLOCK_X
else:
ELSE_CODE_BLOCK
Example:
def ask_computer(a, b):
if a > b:
print("Computer says no")
elif a == b:
print("Computer says yes")
else:
print("Computer says maybe")
ask_computer(1, 2)
ask_computer(2, 2)
ask_computer(3, 2)