(python)企业发放的奖金根据利润来确定提成比例

题目:企业发放的奖金根据利润来确定提成比例。
利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?

看到这道题,俺的思路是这样滴!
|
|
|
在这里插入图片描述
然后很直白的写出代码:
如下

x=float(input("please enter the profit:"))
if x<=10:
    sum=x*0.1
if x>10 and x<=20:
    sum=10*0.1+(x-10)*0.075
if x>20 and x<=40:
    sum=10*0.1+(20-10)*0.075+(x-20)*0.05
if x>40 and x<=60:
    sum=10*0.1+(20-10)*0.075+(40-20)*0.05+(x-40)*0.03
if x>60 and x<=100:
    sum=10*0.1+(20-10)*0.075+(40-20)*0.05+(60-40)*0.03+(x-60)*0.015
if x>100:
    sum=10*0.1+(20-10)*0.075+(40-20)*0.05+(60-40)*0.03+(100-60)*0.015+(x-100)*0.01
print(sum)

这应该一看就会,就不解释了

或者加上函数:
这样:

x=float(input("please enter the profit:"))
def aa(profit):
    if x<=10:
        return x*0.1
    elif x>10 and x<=20:
        return 10*0.1+(x-10)*0.075
    elif x>20 and x<=40:
        return 10*0.1+(20-10)*0.075+(x-20)*0.05
    elif x>40 and x<=60:
        return 10*0.1+(20-10)*0.075+(40-20)*0.05+(x-40)*0.03
    elif x>60 and x<=100:
        return 10*0.1+(20-10)*0.075+(40-20)*0.05+(60-40)*0.03+(x-60)*0.015
    elif x>100:
        return 10*0.1+(20-10)*0.075+(40-20)*0.05+(60-40)*0.03+(100-60)*0.015+(x-100)*0.01
sum=aa(x)
print("the profit is ",sum)

但以上的代码确实不太精简
所以,还有一种方法,就是用数组让它精简一下

从上面代码我们可以发现,上面利润的计算方法有一个通式
就是(a-b)*c————如:10 * 0.1=(10-0)*0.1
那么,我们就可以按照这个公式精简代码

x=float(input("please enter the profit:"))
a= [100,60,40,20,10,0]
b= [0.01,0.015,0.03,0.05,0.075,0.1]
sum=0
for i in range(0,6):
    if x>a[i]:
        sum+= (x-a[i])*b[i]#公式
        x=a[i]
print("the profit is ",sum)

暂时只能想到这么多…
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44797539/article/details/104111296