首页 > 编程语言 > 详细

python 代码一模一样,出现运行结果不同的情况

时间:2020-06-09 01:19:54      阅读:81      评论:0      收藏:0      [点我收藏+]

下面代码:

money = 3
total_money = 0
for i in range(40):#一个月坐车40次
if total_money < 100:#单程100内不打折
total_money += money

elif total_money >= 100 and total_money < 150:#100到150打8折
total_money += money * 0.8

elif total_money >= 150 and total_money < 400:#150到400打5折
total_money += money * 0.5

elif total_money >= 400:#超过400后不打折
total_money += money

print("小明一个月坐车车费:%2.f" % total_money)

 

 

 运行结果:小明一个月坐车车费:116

下面代码:

total_money = 0
money = 3
for i in range(40):
if total_money < 100:
total_money += money
elif total_money >= 100 and total_money < 150:
total_money += money * 0.8
elif total_money >= 150 and total_money < 400:
total_money += money * 0.5
elif total_money >= 400:
total_money += money
print(‘小明消费:%.2f‘ % total_money)

 运行结果:小明消费:116.40

最后发现是个乌龙,大意了一点点,犯了个低级错误

是最后的格式化输出%.2f写成了%2.f

当引以为戒

python 代码一模一样,出现运行结果不同的情况

原文:https://www.cnblogs.com/will-wu/p/13069690.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!