Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Compute Ticket Price with Multiple Rules

Compute the final ticket price applying multiple, ordered discounts and surcharges.

Python practice28 minControl Flow (If–Else Logic)AdvancedLast updated March 15, 2026

Problem statement

Write a function compute_ticket_price(age, is_student, day_of_week, early_bird) that calculates the final ticket price based on multiple rules. Pricing rules (apply in this order): 1. Base price is $20.0. 2. If age < 5, the ticket is free: return 0.0 immediately. 3. Apply either the student discount or the senior discount (they do not combine): - If is_student is True, apply a 50% discount on the current price. - Else if age >= 65, apply a 30% discount on the current price. - Otherwise no percentage discount here. Student discount takes precedence over the senior discount when both would apply. 4. If the day_of_week (case-insensitive) is "wednesday", apply an additional 10% discount to the current price. 5. If the day_of_week is "saturday" or "sunday" (case-insensitive), add a fixed weekend surcharge of $5 to the current price. 6. If early_bird is True, subtract $2 from the current price. 7. Ensure the final price is not negative. Round the final price to 2 decimal places and return it as a float. Examples below show the step-by-step application of the rules.

Task

Practice multi-branch conditional logic and ordering of rules: precedence, cumulative percentage discounts, fixed surcharges, and floor rules.

Examples

Student on Wednesday with early bird

Input

compute_ticket_price(20, True, 'Wednesday', True)

Output

7.0

Base 20 -> student 50% -> 10 -> wednesday 10% -> 9 -> early bird -2 -> 7. Final: 7.0

Input format

Arguments: age (int), is_student (bool), day_of_week (string), early_bird (bool).

Output format

A float representing the final ticket price rounded to 2 decimals.

Constraints

- Base price fixed at 20.0. - Follow rule order exactly (precedence matters). - day_of_week comparisons are case-insensitive. - Never return a negative price; minimum is 0.0. - Return a float rounded to 2 decimal places.

Samples

Sample 1

Input

compute_ticket_price(70, False, 'Sunday', False)

Output

19.0

Base 20 -> senior 30% -> 14 -> weekend +5 -> 19. Final: 19.0