Discount on a bill

EasyIf-Else Programs · 52 of 64

A shop gives 20% off bills above 2000 and 10% off bills above 1000. Write a Python program to calculate the discount.

Advertisement
Input
1500, 2500, 800
Output
Bill 1500: discount 150.0, pay 1350.0
Bill 2500: discount 500.0, pay 2000.0
Bill 800: discount 0, pay 800

Approach

  1. Check the bigger threshold first.

Solution

Python
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}")
Output
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

Lines 2-5

Testing > 2000 first stops a 2500 bill from getting only 10%.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs