Python练习100题二

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

分析:根据题目,可以将此题目利用分段函数处理,需要使用if-elif-else,方式处理。

profit = int(input('Please input the profit:\n'))
# 通过input输入的是str,需要通过int()函数将其转化为数据
bonus_percent_Level1 = 100000 * 0.1
bonus_percent_Level2 = 100000 * 0.175
bonus_percent_Level3 = 100000 * 0.275
bonus_percent_Level4 = 100000 * 0.335
bonus_percent_Level5 = 100000 * 0.395
# 预定义相关的变量便于后续处理

if profit <= 100000:
    bonus_percent = profit * 0.1
elif profit <= 200000:
    bonus_percent = bonus_percent_Level1 + (profit - 100000) * 0.075
elif profit <= 400000:
    bonus_percent = bonus_percent_Level2 + (profit - 200000) * 0.05
elif profit <= 600000:
    bonus_percent = bonus_percent_Level3 + (profit - 400000) * 0.03
elif profit <= 1000000:
    bonus_percent = bonus_percent_Level4 + (profit - 600000) * 0.015
else:
    bouns_percent = bonus_percent_Level5 + (profit - 1000000) * 0.01

print('Bonus_percent: {}'.format(bonus_percent))

知识点:
1.int()函数:
int() 函数用于将一个字符串或数字转换为整型。
语法: class int(x, base=10)
参数: x – 字符串或数字。如果不传参数时,得到结果为0
base – 进制数,默认十进制。

PyDev console: starting.
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32
int()
0
int(3.6)
3
int('12',16)
18
int('0xa',16)
10
int('10',8)
8
int('10',10)
10
int(100,10)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: int() can't convert non-string with explicit base
# 如果参数X为数据类型,就不能给base设置参数,否则会出错。因此,如果要给base设置类型时,X参数必须为string类型。

format函数格式化输出:
一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
基本语法是通过 {} 和 : 来代替以前的 % 。
format 函数可以接受不限个参数,位置可以不按顺序,在{}中的数字代表的是format中参数的顺序,并且此参数可以重复使用。在{}中的变量代表format中的参数。

name = 'Mary'
age = 18
s = "{0}\'s age is {1}".format(name,age)
print(s)
Mary's age is 18

>>>"{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
'hello world'
 
>>> "{0} {1}".format("hello", "world")  # 设置指定位置
'hello world'
 
>>> "{1} {0} {1}".format("hello", "world")  # 设置指定位置
'world hello world'

对于数字格式化,消息参考:https://www.runoob.com/python/att-string-format.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值