python 3 代码一模一样,出现运行结果不同的情况(只是不以为一样而已)


下面代码:

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

当引以为戒


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM