python 题目2:企业发放的奖金根据利润来确定提成比例。

自己写的,太繁琐

i = float(input("请输入当月利润:"))
p = 0          
if i <= 10:
    p = i * 0.1
    print("应发放奖金总数为 %0.2f 万元:"% p)

elif i > 10 and i < 20:
    p = 10 * 0.1 + (i - 10) * 0.075
    print("应发放奖金总数为 %0.2f 万元:"% p)
elif i >= 20 and i < 40:
    p = 10 * 0.1 + 10 * 0.075 + (i - 20) * 0.05
    print("应发放奖金总数为 %0.2f 万元:"% p)
elif i >= 40 and i < 60:
    p = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40) * 0.03
    print("应发放奖金总数为 %0.2f 万元:"% p)
elif i >= 60 and i < 100:
    p = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 +  20 * 0.03 + (i - 60) * 0.015
    print("应发放奖金总数为 %0.2f 万元:"% p)
elif i >= 100:
    p = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100) * 0.01
    print("应发放奖金总数为 %0.2f 万元:"% p)

下面数组方式,来自 @涛啊!

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)

你可能感兴趣的:(python)