Module 8: Advanced Practice

Applying `elif` with more complex logic and scenarios.

Complex Condition Challenge

Write Python code using `if`/`elif`/`else` and logical operators (`and`, `or`) for the following movie ticket scenario.

Scenario: Calculate ticket price based on `age` and `is_student` (True/False):
- **Free:** Age under 5.
- **$8 (Student):** Age 5-17 OR `is_student` is True.
- **$10 (Senior):** Age 65 or older AND NOT a student.
- **$15 (Adult):** Everyone else.

Assume `age` (number) and `is_student` (boolean) are defined. Print only the final price (e.g., `print(8)`).
Output appears here...

Tiered Discount Logic

Implement a discount system based on `purchase_amount`. Order matters!

Scenario: Given `purchase_amount`, calculate and print the `discount_percentage`:
- **20%:** Amount >= $1000
- **15%:** Amount >= $500 (but less than $1000)
- **10%:** Amount >= $100 (but less than $500)
- **5%:** Amount >= $50 (but less than $100)
- **0%:** Amount < $50

Assume `purchase_amount` is defined. Print only the percentage number (e.g., `print(15)`).
Output appears here...

Refactoring Conditionals

The code below works, but it can be made clearer and more concise using `elif`. Rewrite it in the editor.

Original Code (Verbose):

status = "warning"
message = ""

if status == "ok":
    message = "System nominal."
else:
    if status == "warning":
        message = "Check system logs."
    else:
        if status == "error":
            message = "Critical error!"
        else:
            message = "Unknown status."

print(message)

Your Refactored Code:

Debugging Conditionals

Find and fix the logical error in the code below. The goal is to classify a number as "Small", "Medium", or "Large".

Problem: The following code should print "Small" if `num < 10`, "Medium" if `num < 100`, and "Large" if `num >= 100`. However, it sometimes gives the wrong output (e.g., for `num = 5`).
# --- Buggy Code ---
num = 5 # Example value

if num < 100: # Condition 1
    print("Medium")
elif num < 10:  # Condition 2
    print("Small")
else:           # Condition 3
    print("Large")
Hint: Remember Python checks conditions in order. How does `num = 5` evaluate against the first condition (`num < 100`)?

Advanced Practice Complete!

Fantastic job tackling these more complex conditional scenarios! You're effectively using `elif` and logical operators.
Solidify your understanding by reviewing the Standard Practice or watching the Video Lesson.