2015-09-17 53 views
1

我正在为CS课程修改计算器脚本。代码如下:Python中的'//'运算符没有返回正确的解决方案

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

+0

这仅仅是java的python部分。 –

+1

'//'在Python2.7中进行了floor division,并且等于'/'。机会是,你的价值只有20以下,并且你将它视为一个整数,因为它是如何存储的。 –

+1

当出现这种情况时,你必须检查你的假设。第一步当然是弄清楚你的假设是什么。在这种情况下,'changeInCents'在您确定为有问题的行之前是20。 –

回答

3

如果您检查变量,你会看到这个问题

>>> changeInCents 
19.999999999999886 
>>> centValueOfDime 
10 

这是由于浮点精度有限。

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

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

还检查了内置divmod()功能

+0

这确实是问题,谢谢。我做了什么来解决它是使用round()它到最接近的零。我很感激! – Eric

2

这是由于浮点精度问题。试试这个:

purchaseTotal = 13.3 
moneyPaid = 20 

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

你可能希望change0.7,但实际上它是一个数量非常接近0.7,但不准确。最后,changeInCents得到的数字预计为20.0,但实际上有点小。

+0

要解决这个问题 - 将所有计算都作为美分 - 然后转换回美元/美分。 –

+0

所有计算都以美分完成@ TonySuffolk66,但它们也是以浮点形式完成的。 –

+0

@JoshCaswell好点 - 呃 –

0

我试图调试你的代码,但其他队友已经发现了它。 IEEE 754标准中存储数字的方式存在问题(您可以了解更多关于它的信息here)。

我对你的建议是用十进制工作(读here

这是非常简单的,

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

阅读文档,它允许您设置如何了很多相关的问题整理数字。

+2

我认为十进制在这里是过度的 - 特别是刚刚起步的学生,但是对于一般问题来说,这是一个很好的解决方案。 –

+0

我的确同意,他似乎只是从javaland转移而来。但我想这会让他花费更多的时间和精力去理解这个类,而不是实际上通过数字的整个浮点表示。 –