Nov-04-2020, 10:00 AM
To enhance learning process I introduce alternative approach.
This has some advantages - if discounts/breakpoints change you need to change only appropriate list and all code logic will remain the same:
- create list with discounts breakpoints
- create list with discounts
- iterate pairwise and find where purchased quantity is less than discount breakpoint:
- if found - assing discount and break out of loop
- if not found (no-break) - assign maximum discount
This has some advantages - if discounts/breakpoints change you need to change only appropriate list and all code logic will remain the same:
purchased = 99 quantities = [10, 20, 50, 100] discounts = [0, 20, 30, 40, 50] for quantity, discount in zip(quantities, discounts): if purchased < quantity: applied_discount = discount break else: # no-break applied_discount = discounts[-1] print(applied_discount) -> 40