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,求应发放奖金总数?
"""
import re def calculate1(profit):
"""
自己的解答,直接使用最简单的思路
"""
print("奖金计算一的结果", end=":")
if profit < 0:
print("利润不能小于0")
elif 0 <= profit <= 100000:
print("%.2f*0.1=%.2f" % (profit, profit * 0.1))
elif 100000 < profit <= 200000:
print("(%.2f-100000)*0.075+10000=%.2f" % (profit, (profit - 100000) * 0.075 + 10000))
elif 200000 < profit <= 400000:
print("(%.2f-200000)*0.05+10000+7500=%.2f" % (profit, (profit - 200000) * 0.05 + 10000 + 7500))
elif 400000 < profit <= 600000:
print("((%.2f-400000)*0.03+10000+7500+10000=%.2f" % (profit, (profit - 400000) * 0.03 + 10000 + 7500 + 10000))
elif 600000 < profit <= 1000000:
print("((%.2f-600000)*0.015+10000+7500+10000+6000=%.2f" % (profit, (profit - 600000) * 0.015 + 10000 + 7500 +
10000 + 6000))
else:
print("((%.2f-1000000)*0.01+10000+7500+10000+6000+6000=%.2f" % (profit, (profit - 1000000) * 0.01 + 10000 + 7500
+ 10000 + 6000 + 6000)) def calculate2(profit):
"""
参考答案,通过循环已排序好的阶段值列表,来计算总奖金
"""
print("奖金计算二的结果", end=":")
if profit < 0:
print("利润不能小于0")
return
profitRank = (1000000, 600000, 400000, 200000, 100000, 0)
profitRate = (0.01, 0.015, 0.03, 0.05, 0.075, 0.1)
tempProfit = profit
sumBonus = 0
for i in range(0, 6):
if tempProfit > profitRank[i]:
rankBonus = (tempProfit - profitRank[i]) * profitRate[i]
sumBonus += rankBonus
tempProfit = profitRank[i]
if i == 5:
print("%.2f" % rankBonus, end="=")
else:
print("%.2f" % rankBonus, end="+")
print("%.2f" % sumBonus) def calculate3(profit):
"""
利用sum方法计算,总金额, 其中还用到了zip,map,filter,join等函数
"""
print("奖金计算三的结果", end=":")
if profit < 0:
print("利润不能小于0")
return
profitRank = [1000000, 600000, 400000, 200000, 100000, 0]
profitRate = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1]
profitCalc = [0, 0, 0, 0, 0, 0]
tempProfit = profit
for i in range(0, 6):
if tempProfit > profitRank[i]:
profitCalc[i] = tempProfit - profitRank[i]
tempProfit = profitRank[i]
pList = zip(profitCalc, profitRate)
bonusList = map(lambda p: p[0] * p[1], pList)
bonusList = list(filter(lambda f: f > 0, bonusList))
bonus = sum(bonusList)
bonusStrList = map(lambda f: "{:.2f}".format(f), bonusList)
print("%s=%.2f" % ("+".join(bonusStrList), bonus)) def calculate4(profit):
"""
利用列表的切片来实现计算
:param profit:
:return:
"""
print("奖金计算四的结果", end=":")
if profit < 0:
print("利润不能小于0")
return
profitRank = [1000000, 600000, 400000, 200000, 100000, 0]
profitRate = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1]
profitCalc = [400000, 200000, 200000, 100000, 100000]
for i in range(0, 6):
if profit > profitRank[i]:
profitRate = profitRate[i:]
profitCalc = profitCalc[i:]
profitCalc.insert(0, profit - profitRank[i])
break
pList = zip(profitCalc, profitRate)
bonusList = map(lambda p: p[0] * p[1], pList)
bonusList = list(filter(lambda f: f > 0, bonusList))
bonus = sum(bonusList)
bonusStrList = map(lambda f: "%.2f" % f, bonusList)
print("%s=%.2f" % ("+".join(bonusStrList), bonus)) def calculate5(profit):
"""
利用嵌套列表实现,其实和calculate2差不多,只不过是把两个列表组合到一个嵌套列表中
:param profit:
:return:
"""
print("奖金计算五的结果", end=":")
if profit < 0:
print("利润不能小于0")
return
profitRank = [[1000000, 0.01], [600000, 0.015], [400000, 0.03], [200000, 0.05], [100000, 0.075], [0, 0.1]]
profitTemp = profit
bonus = 0
for i in range(0, 6):
if profitTemp > profitRank[i][0]:
bonusRank = (profitTemp - profitRank[i][0]) * profitRank[i][1]
bonus += bonusRank
profitTemp = profitRank[i][0]
if i == 5:
print("%.2f" % bonusRank, end="=")
else:
print("%.2f" % bonusRank, end="+")
print("%.2f" % bonus) def calculate6(profit):
"""
利用字典计算奖金,类似calculate5,只是将嵌套列表换成字典
中间用到了,列表的排序方法sort和倒序方法reverse
:param profit:
:return:
"""
print("奖金计算六的结果", end=":")
if profit < 0:
print("利润不能小于0")
return
profitDict = {1000000: 0.01, 600000: 0.015, 400000: 0.03, 200000: 0.05, 100000: 0.075, 0: 0.1}
# dict.keys()返回的事迭代器
profitKey = list(profitDict.keys())
profitKey.sort()
profitKey.reverse()
profitTemp = profit
bonus = 0
for i in range(0, len(profitKey)):
if profitTemp > profitKey[i]:
bonusRank = (profitTemp - profitKey[i]) * profitDict[profitKey[i]]
bonus += bonusRank
profitTemp = profitKey[i]
if i == len(profitKey) - 1:
print("%.2f" % bonusRank, end="=")
else:
print("%.2f" % bonusRank, end="+")
print("%.2f" % bonus) def calculate7(profit, s):
"""
利用递归来计算奖金,类似calculate1
:param s:
:param profit:
:return:
"""
if s == 0:
print("奖金计算七的结果", end=":")
if profit < 0:
print("利润不能小于0")
return
elif 0 <= profit <= 100000:
bonus1 = bonus = profit * 0.1
elif 100000 < profit <= 200000:
bonus1 = (profit - 100000) * 0.075
bonus = bonus1 + calculate7(100000, 1)
elif 200000 < profit <= 400000:
bonus1 = (profit - 200000) * 0.05
bonus = bonus1 + calculate7(200000, 1)
elif 400000 < profit <= 600000:
bonus1 = (profit - 400000) * 0.03
bonus = bonus1 + calculate7(400000, 1)
elif 600000 < profit <= 1000000:
bonus1 = (profit - 600000) * 0.015
bonus = bonus1 + calculate7(600000, 1)
else:
bonus1 = (profit - 1000000) * 0.01
bonus = bonus1 + calculate7(1000000, 1)
if s == 0:
print("%.2f" % bonus1, end="=")
print("%.2f" % bonus)
else:
print("%.2f" % bonus1, end="+")
return bonus def answer():
"""
解题答案,输入利润,然后调用计算方法计算利润
不过在计算前,需要判断一下输入的是数值类型的,因为input输入的都是str类型 str为字符串 str.isalnum() 所有字符都是数字或者字母
str.isalpha() 所有字符都是字母
str.isdigit() 所有字符都是数字
str.islower() 所有字符都是小写
str.isupper() 所有字符都是大写
str.istitle() 所有单词都是首字母大写,像标题
str.isspace() 所有字符都是空白字符、\t、\n、\r
上述的主要是针对整型的数字,但是对于浮点数来说就不适用了,那么浮点数怎么判断呢,
1.通过异常来判断,try: f = float(str) exception ValueError: print("输入的不是数字!")
2.通过正则表达式来判断:'^[-+]?[0-9]+(\.[0-9]+)?$
"""
profit = input("输入利润金额:")
if profit == "q":
return
reg = re.compile('^[-+]?[0-9]+(\.[0-9]+)?$')
result = reg.match(profit)
if result:
profitFloat = float(profit)
calculate1(profitFloat)
calculate2(profitFloat)
calculate3(profitFloat)
calculate4(profitFloat)
calculate5(profitFloat)
calculate6(profitFloat)
calculate7(profitFloat, 0)
else:
print("Error:输入的不是数值。")
print("继续,或输入q推出")
answer() answer()
python学习——练习题(2)的更多相关文章
- python学习——练习题(10)
""" 题目:暂停一秒输出,并格式化当前时间. """ import sys import time def answer1(): &quo ...
- python学习——练习题(9)
""" 题目:暂停一秒输出. 程序分析:使用 time 模块的 sleep() 函数. http://www.runoob.com/python/python-date- ...
- python学习——练习题(6)
""" 题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21 ...
- python学习——练习题(4)
""" 题目:输入某年某月某日,判断这一天是这一年的第几天? """ import datetime import time from fu ...
- python学习——练习题(1)
""" 题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? """ import itertools d ...
- python学习——练习题(13)
""" 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个" ...
- python学习——练习题(12)
""" 题目:判断101-200之间有多少个素数,并输出所有素数. 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外 ...
- python学习——练习题(11)
""" 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1 1 2 ...
- python学习——练习题(8)
""" 题目:输出 9*9 乘法口诀表. """ def answer1(): """ 自己用最普通的双重循环 ...
- python学习——练习题(7)
""" 题目:将一个列表的数据复制到另一个列表中. """ import copy def validate(a, b): "&q ...
随机推荐
- Python之paramiko
一.基础 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Solaris, BSD, MacOS X ...
- CPU高获取其线程ID然后分析
一.具体步骤 shift+p 按照cpu排序 shift+m按照内存排序 1.查看进程下所有线程 top -H -p pid 2.将十进制数换成16进制:print "%x/n" ...
- Django和pymysql搭建学员管理系统
学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...
- Linux使用sshfs通过ssh挂载远端机器
Linux使用sshfs通过ssh挂载远端机器 今天自己的开发机器突然磁盘剩余空间报警,再弄一块硬盘不太现实,于是想到了公司有一台机器上挂了一个大的磁盘,于是,我把目标偷偷锁定到了那个机器上. 折腾了 ...
- MySql必知必会实战练习(五)存储过程
1. 为什么使用存储过程? (1) 通过把处理封装在容易使用的单元中,简化复杂操作 (2) 为了保证数据的完整性,不要求反复建立一系列的处理操作,所有开发人员和应用程序都使用同一(试验和测试)存储过程 ...
- mybatis 联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- ehchache和redis
Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS中取出来的高花费.高延迟采取的一种缓存方案.正因为Ehcache具有健壮性(基于java开发).被认证(具有apa ...
- 模糊聚类算法(FCM)
伴随着模糊集理论的形成.发展和深化,RusPini率先提出模糊划分的概念.以此为起点和基础,模糊聚类理论和方法迅速蓬勃发展起来.针对不同的应用,人们提出了很多模糊聚类算法,比较典型的有基于相似性关系和 ...
- 【转】IUSR和IIS_IUSRS
转自:http://blog.chinaunix.net/uid-20344928-id-3306130.html 概述 在早期的IIS版本中,随着IIS的安装,系统会创建一个IUSR_Mac ...
- Servlet、Filter、Listener
1.Servlet 1.1servlet接口 All Known Implementing Classes:GenericServlet, HttpServlet GenericServlet:与协议 ...