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