gpt4 book ai didi

python - Python中的'//'运算符没有返回正确的解决方法

转载 作者:太空宇宙 更新时间:2023-11-04 08:53:37 24 4
gpt4 key购买 nike

我正在为我的 CS 类(class)编写更改计算器脚本。代码如下:

# Set up our money variables
centValueOfTenDollarBill = 1000
centValueOfFiveDollarBill = 500
centValueOfToonie = 200
centValueOfLoonie = 100
centValueOfQuarter = 25
centValueOfDime = 10
centValueOfNickel = 5

# Set up our variables
purchaseTotal = input("Enter purchase total: ") # Purchase costs $12.50
moneyPaid = input("Enter money paid: ") # Customer gives cashier $20.00

# Figure out the change
change = moneyPaid - purchaseTotal

# Echo input data to user
print("""The total of the purchase is $%0.2f.
The customer paid $%0.2f.
The cashier gives $%0.2f back to the customer in the following fashion: """ %(purchaseTotal, moneyPaid, change))

#Convert dollars into cents to facilitate the computation
purchaseTotalInCents = purchaseTotal * 100
moneyPaidInCents = moneyPaid * 100
changeInCents = change * 100

# Determine # of $10 to be given back as part of the change
numberOfTenDollarBills = changeInCents // centValueOfTenDollarBill
changeInCents = changeInCents - (centValueOfTenDollarBill * numberOfTenDollarBills)

# Determine # of $5 to be given back as part of the change
numberOfFiveDollarBills = changeInCents // centValueOfFiveDollarBill
changeInCents -= (centValueOfFiveDollarBill * numberOfFiveDollarBills)

# Determine # of $2 (toonies) to be given back as part of the change
numberOfToonieCoins = changeInCents // centValueOfToonie
changeInCents -= (centValueOfToonie * numberOfToonieCoins)

# Determine # of $1 (loonies) to be given back as part of the change
numberOfLoonieCoins = changeInCents // centValueOfLoonie
changeInCents -= (centValueOfLoonie * numberOfLoonieCoins)

# Determine # of $0.25 (quarters) to be given back as part of the change
numberOfQuarterCoins = changeInCents // centValueOfQuarter
changeInCents -= (centValueOfQuarter * numberOfQuarterCoins)

# Determine # of $0.10 (dimes) to be given back as part of the change
numberOfDimeCoins = changeInCents // centValueOfDime #<--- PROBLEM HERE IF DIMES ARE TWO
print (numberOfDimeCoins)
changeInCents -= (centValueOfDime * numberOfDimeCoins)

# At this point, changeInCents can either be
# 5 -> 1 x $0.05 (nickels) or
# 0 -> 0 x $0.05 (nickels)
numberOfNickelCoins = changeInCents // centValueOfNickel

# Output the result: change cashier needs to give back to customer
print("\t%i x $10.00" %numberOfTenDollarBills)
print("\t%i x $ 5.00" %numberOfFiveDollarBills)
print("\t%i x $ 2.00" %numberOfToonieCoins)
print("\t%i x $ 1.00" %numberOfLoonieCoins)
print("\t%i x $ 0.25" %numberOfQuarterCoins)
print("\t%i x $ 0.10" %numberOfDimeCoins)
print("\t%i x $ 0.05" %numberOfNickelCoins)

# Indicates the end of execution
print("----\n")

所有错误的地方(至少从我看来是这样)是,如果该程序应该返还两角钱,它会返还一角钱和一枚五分钱,这给客户换了五美分。如果它应该回馈一毛钱,那就没问题了。

示例:假设客户为一件 13.30 美元的商品支付了 20 美元。零钱是 6.70 美元。

numberOfDimeCoins = changeInCents // centValueOfDime

上面这一行应该与 2.0 = 20.0//10.0 相同,但它返回 1.0

如果您支付了 20 美元购买任何需要回馈一毛钱的东西,那么一切都是正确的,例如 13.20 美元、13.90 美元或 13.75 美元的元素。

下面是一些示例输出:

Erics-MacBook-Pro:Desktop eric$ python change.py
Enter purchase total: 13.75
Enter money paid: 20
The total of the purchase is $13.75.
The customer paid $20.00.
The cashier gives $6.25 back to the customer in the following fashion:
0 x $10.00
1 x $ 5.00
0 x $ 2.00
1 x $ 1.00
1 x $ 0.25
0 x $ 0.10
0 x $ 0.05
----

Erics-MacBook-Pro:Desktop eric$ python change.py
Enter purchase total: 12.8
Enter money paid: 20
The total of the purchase is $12.80.
The customer paid $20.00.
The cashier gives $7.20 back to the customer in the following fashion:
0 x $10.00
1 x $ 5.00
1 x $ 2.00
0 x $ 1.00
0 x $ 0.25
1 x $ 0.10
1 x $ 0.05
----

我是否遗漏或做错了什么?

使用 Python 2.7。

最佳答案

如果检查变量,就会发现问题

>>> changeInCents
19.999999999999886
>>> centValueOfDime
10

这是由于 float 的精度有限。

您应该将初始值转换为美分。例如。

numberspurchaseTotalInCents = int(purchaseTotal * 100)
moneyPaidInCents = int(moneyPaid * 100)
changeInCents = moneyPaidInCents - numberspurchaseTotalInCents

同时检查内置的 divmod() 函数

关于python - Python中的'//'运算符没有返回正确的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32621058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com