If - Else-if - Else Statement (if-elif-else)
Use for the scenario that has more than two decision-making.
Usage
if meet_condition:
do action
else if meet_condition:
do action
... n else if: ...
do n action
else:
do fallback_action- The condition
mustreturn Boolean value.
Example
if-elif-else.py
- Example Scenario: In a station of theme park, the
game priceisbased on customer's heightandvip status.
py
customers_height = float(input("Please enter your height in cm: "))
vip_status = int(input("Please enter your vip status (1 - 5): "))
# vip status >= 3 or height <= 150cm free to play
if customers_height <= 150:
print("You can play this game for free")
elif vip_status >= 3:
print("You can play this game for free")
else:
print("You have to pay for this game")Results:
Trial 1: (height = 120, vip_level = 1)
You can play this game for freeTrial 2: (height = 170, vip_level = 1)
You have to pay for this gameTrial 3: (height = 170, vip_level = 5)
You can play this game for free