Skip to content

Nested If Statement

For many scenario, it's not only have a multiple decision-making condition, it's also include nested decision making.

Example graph of Nested If statement

Usage

if meet_condition_1:
    do action_1

    if meet_condition:
        do action_2

Example

nested-if.py

  • Example Scenario: In a station of theme park, the game price is based on customer's height and vip status, but vip status only ask when the customer's height is not full-filled.
py
customers_height = float(input("Please enter your height in cm: "))

# vip status >= 3 or height <= 150cm free to play 

if customers_height > 150:
    print("You can play this game for free, if you are vip member")
    vip_status = int(input("Please enter your vip status (1 - 5): "))

    if vip_status >= 3:
        print("Since you are vip, you are free even your height is over")
    else:
        print("You have to pay for this game, amount required to pay - $ 10")

else:
    print("You are for this game, since your height are <= 150 cm")

Results:

Trial 1: (height = 150, vip_level = 0)

Input Required: customers_height

Please enter your height in cm: 150
You are for this game, since your height are <= 150 cm

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

Input Required: customers_height, vip_status

Please enter your height in cm: 170
You can play this game for free, if you are vip member
Please enter your vip status (1 - 5): 1
You have to pay for this game, amount required to pay - $ 10

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

Input Required: customers_height, vip_status

Please enter your height in cm: 170
You can play this game for free, if you are vip member
Please enter your vip status (1 - 5): 5
Since you are vip, you are free even your height is over