Menu

Sign in to track your progress and unlock all features.

Theme style

Start learning freeLog in

Problem No 17

Apply Discount Based on Total

Medium

≈ 16 minute session

Summary

Apply tiered discounts to a purchase total and return the final amount rounded to two decimals.

Problem statement

Write a function apply_discount(total) that applies a discount based on the total amount (a non-negative number): - If total >= 500: apply a 20% discount. - Else if total >= 200: apply a 10% discount. - Else if total >= 100: apply a 5% discount. - Otherwise: no discount. Return the final amount rounded to 2 decimal places (use round(amount, 2)).

Task

Use conditional branching to select the correct discount tier and compute the discounted total.

Examples

Apply 5% discount

Input

apply_discount(120)

Output

114.0

Explanation

120 >= 100 so 5% off: 120 * 0.95 = 114.0

Input format

A single number total passed to apply_discount(total).

Output format

A number (float) representing the final total after discount, rounded to two decimals.

Constraints

total is a non-negative float or int. Use round(amount, 2) before returning.

Samples

Sample input 0

apply_discount(250)

Sample output 0

225.0

Explanation 0

250 >= 200 -> 10% discount -> 225.0

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.