Python Forum
Need Help with an Error I'm Getting (Very New to this)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help with an Error I'm Getting (Very New to this)
#11
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020