Write a function in Python to calculate the bonus amount of an employee, require the input of the employee's turnover, and output the corresponding total bonus

The bonus calculation rules are as follows:
when the turnover is less than or equal to 200,000 yuan, the bonus is 11% of the turnover. When the turnover is higher than 200,000 yuan and lower than 400,000 yuan, the part below 200,000 yuan is calculated as 11%, and the part higher than 200,000 yuan is calculated as 8.5%; and so on, between 400,000 and 600,000 3% for the portion above 400,000 yuan; between 600,000 and 1 million, for the portion above 600,000 yuan, 1.5% for the portion above 1 million yuan, for the portion exceeding 1 million yuan Calculated at 1%.


Some insights: When I first typed the code of this question, I just typed it purely according to mathematical thinking, but in the process, I found that I would do a lot of repetitive work, so I thought of using recursion. Let's first look at the calculation process of the bonus for this question from a mathematical point of view.

Let the turnover be x

  • When the turnover is less than or equal to 200,000 yuan (x<=20), the bonus is: 0.11x
  • When the turnover is higher than 200,000 yuan and lower than 400,000 yuan (20<x<=40), the bonus is: 20×0.11+0.85(x-20)
  • When the turnover is between 400,000 and 600,000 (40<x<=60), the bonus is: 20×0.11+20×0.85+0.03(x-40)
  • When the turnover is between 600,000 and 1 million (60<x<=100), the bonus is: 20×0.11+20×0.85+20×0.03+0.015(x-40)
  • When the turnover is higher than 1 million yuan (x>100), the bonus is: 20×0.11+20×0.85+20×0.03+40×0.015+0.01(x-40)

It can be found that to calculate bonuses including different levels of turnover, there are many repeated steps that need to be simply listed one by one. At this time, I thought of using recursion (it happened to be mentioned in this part of the class), so I decided to write a recursive test myself. try. The popular understanding of recursion here can imagine each conditional statement as an entry, and the return statement as an exit. The conditional statement determines which door should be entered, and calling itself in the return is equivalent to going through a different door when it comes out, so using recursion is much more concise than the code I wrote before. The general idea is as follows:insert image description here

Here is the final code:

def Cal_bonus(n: int) -> float:
    """按营业额不等份额分段计算奖金,输出每段奖金数和奖金总额"""

    # 营业额低于或等于20万元时
    if n <= 20:
        m = n
        b = m * 0.11
        print('*金额{:>2}万,比率{:>6},奖金{:.2f}万'.format(int(m), '11.00%', b))
        # 由于数字的长度不同,所以为了美观打印,数字单独需要右对齐,奖金保留两位小数,以下方法相同
        return b

    # 营业额高于20万元,低于40万元时
    elif 20 < n <= 40:
        m = n - 20
        b = m * 0.085
        print('*金额{:>2}万,比率{:>6},奖金{:.2f}万'.format(int(m), '8.50%', b))
        return Cal_bonus(20) + b

    # 营业额高于40万元,低于60万元时
    elif 40 < n <= 60:
        m = n - 40
        b = m * 0.03
        print('*金额{:>2}万,比率{:>6},奖金{:.2f}万'.format(int(m), '3.00%', b))
        return Cal_bonus(40) + b

    # 营业额高于60万元,低于100万元时
    elif 60 < n <= 100:
        m = n - 60
        b = m * 0.015
        print('*金额{:>2}万,比率{:>6},奖金{:.2f}万'.format(int(m), '1.50%', b))
        return Cal_bonus(60) + b

    # 营业额高于100万元时
    else:
        m = n - 100
        b = m * 0.01
        print('*金额{:>2}万,比率{:>6},奖金{:.2f}万'.format(int(m), '1.00%', b))
        return Cal_bonus(100) + b


def main():
    turnover = float(input('请输入营业额(万元):'))
    print('******奖金总计为:%.2f万元******' % Cal_bonus(turnover))


if __name__ == "__main__":
    main()

The calculation results are as follows:
Input 14:
insert image description here
Input 36:
insert image description here
Input 58:
insert image description here
Input 99:
insert image description here
Input 101:
insert image description here

Guess you like

Origin blog.csdn.net/m0_65723013/article/details/127729109
Recommended