Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

python - Sales commission calculator

I'm trying to create a sales commission calculator. These are the roles:

Tiers |       Amount        | Percentage applied
  1   | £84,000 - £98,000   | 7.50%
  2   | £98,000 - £112,000  | 10.00%
  3   | £112,000 - £126,000 | 12%
  4   | £126,000- £146,000  | 15.00%

minimum amount= 84k

This is my code, but it doesn't return any value

from bisect import bisect

rates = [0, 7.5, 10, 12, 15]   


brackets = [84000,        # first 84k
            98000,        # next  14k
            112000,       # next  14k
            126000,       # next  14k
            146000]       # next  20k


base_tax = [0,            # 84k * 0%
            14000,        # 14k * 7.5%
            14000,        # 14k * 7.5% + 14000
            14000,        # 14k * 7.5% + 14000 + 14000
            20000]        # 20k * 15% + 14000 + 14000 + 14000

income = 120000

def tax(income):
    i = bisect(brackets, income)
    if not i:
        return 0
    rate = rates[i]
    bracket = brackets[i-1]
    income_in_bracket = income - bracket
    tax_in_bracket = income_in_bracket * rate / 100
    total_tax = base_tax[i-1] + tax_in_bracket

    return total_tax

How can I implement an automatic calculator that takes into account the ratios and the different tires?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • First, you haven't imported the module which contains bisect function.
  • Secondly, in order to return value, the function should be called. You have to call that function. So, you have to do following things:

Add import bisect at the starting of the code.

In i = bisect(brackets, income), you have to write i = bisect.bisect(brackets, income). It's the way to use bisect function of bisect module. The general way of using function of any module is module.function().

To print tax, write print(total_tax) before return code. You haven't written that.

And, at last call the function by adding: tax(income)

P.S. Sorry for this way because the code was not getting formatted properly. Hope you understand.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...