I just started learning programming and I am trying to write a program in Python, that takes the amount that is supposed to be paid and the amount actually paid and calculates, what notes and coins should the cashier give back.
It seems to work fine for everything except the amounts under one dollar. I suspect it has something to do with decimal numbers, but I couldn't find the error. Also, sorry for my code, it's really messy and it could probably be done in a few lines, but I am total beginner.
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()