Python 练习实例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,求应发放奖金总数?
程序分析:请利用数轴来分界,定位。
程序源代码:
实例(Python 2.0+)
#!/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
# -*- 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
实例(Python 3.0+)
#!/usr/bin/python3
i = int(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)
i = int(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
流年细雨
758***[email protected]
使用if...elif...else语句逐一判断
流年细雨
758***[email protected]
Kunz
sun***[email protected]
参考方法:
Kunz
sun***[email protected]
小雨济苍生
27d***163.com
使用切片:
小雨济苍生
27d***163.com
DCGDDD
805***[email protected]
Python中的列表可以嵌套,这样外层列表就跟数组一样,内层的是对象;
不过Python的列表数据类型不一定一样,更加灵活了。
DCGDDD
805***[email protected]
仰望星空-走向深蓝
910***[email protected]
使用字典控制利润与提成比例的匹配:
仰望星空-走向深蓝
910***[email protected]
newbie2014
209***[email protected]
Python3 测试方法:
newbie2014
209***[email protected]
阳光不锈
173***[email protected]
可以同时使用两种方式创建生成器: 生成器推导式 与 使用yield关键字构造生成器函数, 如下所示:
阳光不锈
173***[email protected]