Python Nested If · Exercises
Beginner

Python Nested If Exercises

4 exercises that build on the Python Nested If lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: A nested if is an if statement placed inside another if (or else) block, so a second question is only asked after the first one has been answered. By the end you can write nested if statements with correct indentation, decide when nesting is the natural choice, put an elif ladder inside an if, and flatten deep nesting with and or by checking the problems first. You will practise: Ticket first, then seat; Follow the nested flow; Fix the member discount; Order status tracker.

0 / 4 completed

Python Nested If practice exercises

1. Ticket first, then seat

Write code Easy

A cinema only asks about the seat once the customer has a ticket. Write a nested if that sets message:

  • no ticket (has_ticket is False): "Buy a ticket first"
  • ticket but seat is an empty string "": "Choose a seat"
  • ticket and a seat: "Enjoy the film"

Then print message. Try other values to see each message, then set them back to has_ticket = True and seat = "" before pressing Check.

Your code
has_ticket = True
seat = ""

# nested if: first check has_ticket, then check seat inside it

2. Follow the nested flow

Predict the output Medium

Read the code and type exactly what it prints. Pay attention to indentation: which else belongs to which if, and which lines are in the same block.

Read this code
score = 72
attended = False

if score >= 50:
    if attended:
        print("Pass")
    else:
        print("Pass, but attendance missing")
    if score >= 90:
        print("Distinction")
    elif score >= 70:
        print("Merit")
else:
    print("Fail")
print("Result recorded")

3. Fix the member discount

Fix the bug Medium

Only members get a discount: 5% on any order, 10% when the total is at least 50, and 20% when it is at least 100. Non-members get 0%.

The ladder below is not nested inside the member check, so non-members with a big order get a discount too. Fix the nesting so the rules above hold.

The code is wrapped in a function so the checker can try many different orders; you'll learn functions properly later. Edit the if statements inside it.

Your code
def discount_percent(is_member, total):
    discount = 0
    if is_member:
        discount = 5
    if total >= 100:
        discount = 20
    elif total >= 50:
        discount = 10
    return discount


print(discount_percent(True, 70))     # should print 10
print(discount_percent(False, 120))   # should print 0

4. Order status tracker

Write code Hard

A warehouse decides what to tell customers about an order:

  • not paid: "Awaiting payment" (nothing else matters)
  • paid but not in stock: "Backordered"
  • paid and in stock, parcel heavier than 20 kg: "Courier pickup" (express makes no difference)
  • paid and in stock, 20 kg or lighter, express: "Ships today"
  • paid and in stock, 20 kg or lighter, not express: "Ships in 3 days"

Complete order_status using nested if statements and store the text in status. Think about which questions only matter after an earlier one was answered.

The code is wrapped in a function so the checker can try every combination; keep the def line and the return status line.

Your code
def order_status(paid, in_stock, express, weight_kg):
    status = ""
    # write your nested if statements here

    return status


print(order_status(True, True, True, 3))    # should print Ships today
print(order_status(False, True, True, 3))   # should print Awaiting payment

Next lessonPython Shorthand If (Ternary)

More Python Nested If examples · All Python exercises