Discount on a bill
A shop gives 20% off bills above 2000 and 10% off bills above 1000. Write a Python program to calculate the discount.
1500, 2500, 800
Bill 1500: discount 150.0, pay 1350.0 Bill 2500: discount 500.0, pay 2000.0 Bill 800: discount 0, pay 800
Approach
- Check the bigger threshold first.
Solution
for bill in [1500, 2500, 800]: if bill > 2000: discount = bill * 0.20 elif bill > 1000: discount = bill * 0.10 else: discount = 0 print(f"Bill {bill}: discount {discount}, pay {bill - discount}")Bill 1500: discount 150.0, pay 1350.0 Bill 2500: discount 500.0, pay 2000.0 Bill 800: discount 0, pay 800
Line by line
Testing > 2000 first stops a 2500 bill from getting only 10%.
Learn the concept
New to this topic? Read the lesson, then come back.