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

当引以为戒

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