Python Forum

Full Version: Need Help with an Error I'm Getting (Very New to this)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
To enhance learning process I introduce alternative approach.

  • 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
Pages: 1 2