Python中的“//”运算符未返回正确的解决方案

2024-10-02 14:17:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的CS课程编写一个变化计算器脚本。代码如下:

# 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")

所有这一切都是错误的(至少从我所看到的)是,如果程序应该返回两个一角硬币,它会返回一个一角硬币和一个五分镍币短的改变客户五美分。如果它应该还一毛钱,那就没问题了。你知道吗

例如:假设一个顾客花了20美元买了一件13.30美元的商品。零钱是6.7美元。你知道吗

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
----

我有什么遗漏或做错的吗?你知道吗

使用Python2.7。你知道吗


Tags: ofthetobackcustomerbepurchasechange
3条回答

我试着调试你的代码,但是其他人已经找到了。您对如何按照ieee754标准存储数字有问题(您可以了解更多)。你知道吗

我建议您使用十进制(阅读here

很直接

#import and set precision
from decimal import *
getcontext().prec = **x** #you decide it
a = Decimal(10)
b = Decimal(0.9)
print a-b

阅读文档,它允许您设置许多有关如何舍入数字的相关问题。你知道吗

这是因为浮点精度问题。试试这个:

purchaseTotal = 13.3
moneyPaid = 20

change = moneyPaid - purchaseTotal
print(repr(change))  # 6.699999999999999

你可能期望change0.7,但事实上这是一个非常接近0.7的数字,但并不精确。最后,changeInCents得到一个您期望的20.0数字,但实际上要小一点。你知道吗

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

>>> changeInCents
19.999999999999886
>>> centValueOfDime
10

这是由于浮点的精度有限。你知道吗

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

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

还可以查看内置的divmod()函数

相关问题 更多 >

    热门问题