|
发表于 2019-8-20 10:09:24
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
麻烦高手将下面的语句逐条解释下。
profit=int(input('Show me the money: '))
bonus=0
thresholds=[100000,100000,200000,200000,400000]
rates=[0.1,0.075,0.05,0.03,0.015,0.01]
for i in range(len(thresholds)):
if profit<=thresholds[i]:
bonus+=profit*rates[i]
profit=0
break
else:
bonus+=thresholds[i]*rates[i]
profit-=thresholds[i]
bonus+=profit*rates[-1]
print(bonus)
- profit=int(input('Show me the money: ')) #将用户给的值赋给profit变量
- bonus=0 #初始化bonus变量
- thresholds=[100000,100000,200000,200000,400000] #创建thresholds列表
- rates=[0.1,0.075,0.05,0.03,0.015,0.01] #创建rates列表
- for i in range(len(thresholds)): # 同等于for i in range(0, 5),因为thresholds列表里有5个元素
- if profit<=thresholds[i]: # 如果profit小于等于threshold列表里的i个元素(第一次循环就是第0个元素)
- bonus+=profit*rates[i] # 将bonus加上profit乘以rates列表里的第i个元素,把最终的值在赋给bonus
- profit=0 # 把profit的数据清空
- break # 跳出for循环
- else:
- bonus+=thresholds[i]*rates[i] # if条件不成立时,bonus = bonus + thresholds的第i个元素乘以rates的第i个元素
- profit-=thresholds[i] # 将profit的值减去threshold的第i个元素,并把值赋给profit
- bonus+=profit*rates[-1] # 最后将bonus的值加上profit的值乘以rates列表的最后一个元素,再把值赋给bonus
- print(bonus) # 最后把bonus的纸打印出来
复制代码
|
|