python2.7练习小例子(二)

时间:2021-08-15 15:15:40

    2):题目:企业发放的奖金根据利润提成。利润(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,求应发放奖金总数?

    程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

    程序源代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*- i = int(raw_input('净利润:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
if i>arr[idx]:
r+=(i-arr[idx])*rat[idx]
print (i-arr[idx])*rat[idx]
i=arr[idx]
print r

    以上实例输出结果为:

净利润:120000
1500.0
10000.0
11500.0

    再来看其他例子:

    使用if...elif...else语句逐一判断:

#!/usr/bin/python
# -*- coding: UTF-8 -*- import sys reload(sys)
sys.setdefaultencoding('utf-8') x = int(raw_input("净利润:")) if x<=100000:
bonus=x*0.1
print u"奖金:",bonus,u"元"
elif 100001<x<=200000:
bonus=10000+(x-100000)*0.075
print u"奖金:",bonus,u"元"
elif 200001<x<=400000:
bonus=10000+7500+(x-200000)*0.05
print u"奖金:",bonus,u"元"
elif 400001<x<=600000:
bonus=10000+7500+10000+(x-400000)*0.03
print u"奖金:",bonus,u"元"
elif 600001<x<=1000000:
bonus=10000+7500+10000+6000+(x-600000)*0.015
print u"奖金:",bonus,u"元"
elif 600001<x<=1000000:
bonus=10000+7500+10000+6000+6000+(x-600000)*0.01
print u"奖金:",bonus,u"元"

    还有一种:

#!/usr/bin/python
# -*- coding: UTF-8 -*- i = int(raw_input('净利润:'))
I = [1000000,600000,400000,200000,100000,0]
r = [0.01,0.015,0.03,0.05,0.075,0.1]
for j in range(len(I)):
if i > I[j]:
b = [0,0,0,0,0,0]
b[j] = i -I[j]
for k in range(j+1,len(I)):
b[k] = I[k-1]
bonus = sum(map(lambda (i1,i2): i1 * i2,zip(b,r)))
break
print '奖金:',bonus

    使用切片:

#!/usr/bin/python
# -*- coding: UTF-8 -*- value=int(raw_input('please input profit: '))
list1=[1000000,600000,400000,200000,100000,0]
list2=[0.01,0.015,0.03,0.05,0.075,0.1]
list3=[400000,200000,200000,100000,100000]
for i in range(6):
if value >list1[i]:
v1=(value-list1[i])*list2[i]
print v1
list2_new=list2[i+1:6]
list3_new=list3[i:5]
v2=sum(map(lambda (x,y):x*y,zip(list2_new,list3_new)))
print v2
print v1+v2
break

    Python中的列表可以嵌套,这样外层列表就跟数组一样,内层的是对象。不过Python的列表数据类型不一定一样,更加灵活了:

#!/usr/bin/python3

Bonus = 0;
BonusRateList = [[100,0.010],[60,0.015],[40,0.030],[20,0.050],[10,0.075],[0,0.100]]; Profit = 120000;
Profit /= 10000; for i in range(0, len(BonusRateList)) :
if (Profit > BonusRateList[i][0]) :
Bonus += ((Profit - BonusRateList[i][0]) * BonusRateList[i][1]);
Profit = BonusRateList[i][0]; print (Bonus * 10000);

    使用字典控制利润与提成比例的匹配:

#!/user/bin/env python
# coding=utf-8 # 计算公司的年度奖金,单位:万元
num = int(raw_input("请输入今年的公司利润:"))
obj = {100: 0.01, 60: 0.015, 40: 0.03, 20: 0.05, 10: 0.075, 0: 0.1}
keys = obj.keys()
keys.sort()
keys.reverse()
r = 0
for key in keys:
if num > key:
r += (num - key) * obj.get(key)
num = key
print "今年的奖金为:", r, "万元。"

    Python3 测试方法:

def  get_reward(I):
    rewards = 0
    if I <= 10:
        rewards = I * 0.1     elif (I > 10) and (I <= 20):
        rewards = (I - 10) * 0.075 + get_reward(10)     elif (I > 20) and (I <= 40):
        rewards = (I - 20) * 0.05 + get_reward(20)     elif (I > 40) and (I <= 60):
        rewards = (I - 40) * 0.03 + get_reward(40)     elif (I > 60) and (I <= 100):
        rewards = (I - 60) * 0.015 + get_reward(60)     else:
        rewards = get_reward(100) + (I - 100) * 0.01     return rewards if __name__ == '__main__':
    i = 120000
    print("净利润:", i)
    print("发放的奖金为:", get_reward(i / 10000) * 10000)

    可以同时使用两种方式创建生成器: 生成器推导式使用yield关键字构造生成器函数, 如下所示:

#使用两种方式创建生成器
a=[100,60,40,20,10,0]
b=[0.01,0.015,0.03,0.05,0.075,0.1] #生成器函数
def f(x):
for i in range(len(a)):
if n>a[i]:
#生成器推导式
c=(a[j]-a[j+1] for j in range(i,len(a)-1))
break
r=sum(map(lambda x,y:x*y,b[i:],[(n-a[i])]+list(c)))
yield r*10000 k=int(input("是否继续计算奖金?是:1, 否:0\n"))
while k:
n=int(input('请输入利润,单位(万元):'))
print('应发奖金为:',next(f(n)),'(元)')
print()
k=int(input("是否继续计算奖金?是:1, 否:0\n"))
print('感谢使用,程序结束!')

最后一个我感觉比较有意思,其它的怎么说呢,万变不离其宗吧。大家可以看下,大神们的代码习惯。如果感觉不错的话,请多多点赞支持哦。。。