Skip to content

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 must return Boolean value.

Example

if-elif-else.py

  • Example Scenario: In a station of theme park, the game price is based on customer's height and vip 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 free

Trial 2: (height = 170, vip_level = 1)

You have to pay for this game

Trial 3: (height = 170, vip_level = 5)

You can play this game for free