Python十进制数



我刚开始学习编程,我正在尝试用Python写一个程序,它需要支付的金额和实际支付的金额,并计算出纳应该退还的纸币和硬币。

除了一美元以下的金额外,它似乎对任何事情都有效。我怀疑它与十进制数有关,但我找不到错误。还有,对不起我的代码,它真的很乱,它可能可以在几行内完成,但我完全是初学者。

amount_due = float(input('What is the amount you are supposed to pay? '))
amount_paid = float(input('What is the amount you paid? '))
one_hundreds = 0
twenties = 0
tens = 0
fives = 0
ones = 0
quarter = 0
dime = 0
nickel = 0
penny = 0
def calculate_difference():
global difference
difference = amount_paid - amount_due
difference = float(difference)
def evaluate_difference():
if difference < 0:
print("Sorry, you haven't paid enough money.")
quit()
elif difference == 0:
print('Awesome! You paid exactly how much you were supposed to pay.')
else: 
check_hundreds()
def check_hundreds():
global one_hundreds
global difference
while difference >= 100:
one_hundreds += 1
difference -= 100
else:
check_twenties()
def check_twenties():
global twenties
global difference
while difference >= 20:
twenties += 1
difference -= 20
else:
check_tens()
def check_tens():
global tens
global difference
while difference >= 10:
tens += 1
difference -= 10
else:
check_fives()
def check_fives():
global fives
global difference
while difference >= 5:
fives += 1
difference -= 5
else:
check_ones()
def check_ones():
global ones
global difference
while difference >= 1:
ones += 1
difference -= 1
else:
check_quarters
def check_quarters():
global quarter
global difference
while difference >= 0.25:
quarter += 1
difference -= 0.25
else:
check_dimes()
def check_dimes():
global dime
global difference
while difference >= 0.1:
dime += 1
difference -= 0.1
else:
check_nickels()
def check_nickels():
global nickel
global difference
while difference >= 0.05:
nickel += 1
difference -= 0.05
else:
check_pennies()
def check_pennies():
global penny
global difference
while difference >= 0.01:
penny += 1
difference -= 0.01
def write_results():
global one_hundreds
global twenties
global tens
global fives
global ones
global quarter
global dime
global nickel
global penny
print('The cashier should return you ' + str(one_hundreds) + " one hundred dollar bills, " + str(twenties) + ' twenty dollar bills, ' + str(tens) + ' ten dollar bills, ' + str(fives) + ' five dollar bills, ' + str(ones) + ' one dollar bills, ' + str(quarter) + ' quarters, ' + str(dime) + ' dimes, ' + str(nickel) + ' nickels and ' + str(penny) + ' pennies.')

calculate_difference()
evaluate_difference()
write_results()

您忘了在check_quarters后面加上()

注:使用全局变量是非常糟糕的编程风格