Я работаю над скриптом калькулятора изменений для моего курса 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")
Все, что не так (по крайней мере, от того, что я могу видеть), что если программа должна вернуть десятицентовики, он возвращает одну монетки и один никель, который короткие изменяют клиент пять центов. Если он должен отдать одну копейку, тогда нет никаких проблем.
Пример: Скажите, что клиент заплатил 20 долларов за пункт 13,30 доллара США. Изменение составляет $ 6,70.
numberOfDimeCoins = changeInCents // centValueOfDime
Эта линия выше должна быть такой же, как 2.0 = 20.0//10.0
, но вместо этого он возвращался 1.0
.
Если вы заплатили 20 долларов за все, где требуется одна копейка, все будет правильно, например, 13,13 долларов США, 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.
Это тоже слишком Java, чтобы быть в разделе python. –
'//' выполняет деление пола в Python2.7 и равно '/'. Скорее всего, ваше значение чуть меньше 20, и вы рассматриваете его как целое число из-за того, как хранятся поплавки. –
Вы должны проверить свои предположения, когда что-то пойдет не так. Первый шаг, конечно, заключается в том, чтобы понять, что такое ваше предположение. В этом случае это означает, что 'changeInCents' - 20 перед линией, которую вы идентифицировали как проблемную. –