Python基础教程100例 练习2

学习Python有一段时间了,从今天开始将Python基础教程100例中的习题完成,运行环境 Python 2.7

参考一个在线教程网站 Python基础教程  ,网址  http://www.runoob.com/python/python-100-examples.html

 

例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,求应发放奖金总数?

题目分析:数学上的函数分段计算,要求有键盘输入

 

比较常规的方法,也可以说是比较笨的方法:

 

# -*- coding: utf-8 -*-

profit = raw_input("Input Profit : ")

bonus1 = 100000*0.1
bonus2 = bonus1 + (200000-100000)*0.075
bonus3 = bonus2 + (400000-200000)*0.05
bonus4 = bonus3 + (600000-400000)*0.03
bonus5 = bonus4 + (1000000-600000)*0.015


if profit <= 100000:
	bonus = profit*0.1
elif profit <= 200000:
	bonus = bonus1 + (profit - 100000)*0.075
elif profit <= 400000:
	bonus = bonus2 + (profit - 200000)*0.05
elif profit <= 600000:
	bonus = bonus3 + (profit - 400000)*0.03
elif profit <= 1000000:
	bonus = bonus4 + (profit - 600000)*0.015
else :
	bonus = bonus5 + (profit - 1000000)*0.001
	
print "The bonus is : %d " % bonus

教程中给出的方法:

 

 

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

 

 

 

 

 

    原文作者:Automation_走天涯
    原文地址: https://blog.csdn.net/u010652755/article/details/49762951
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞