-1

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()
khelwood
  • 55,782
  • 14
  • 81
  • 108
DavidG
  • 9
  • 1
  • The problem is that floating pointer numbers are not always accurate for decimal ones. More about it on [Is math floating point broken?](https://stackoverflow.com/q/588004/3545273). And that is the reason why `Decimal` exists in Python... – Serge Ballesta Feb 22 '21 at 09:16
  • The line ``check_quarters`` inside ``check_ones`` should be ``check_quarters()`` instead. – MisterMiyagi Feb 22 '21 at 09:25

1 Answers1

1

You have forgot to put () after check_quarters

P.S. using globals is very bad programming style

user3431635
  • 372
  • 1
  • 7
  • Thank you very much. Why is that with globals? – DavidG Feb 22 '21 at 09:30
  • When your program has a bug, it usually means that some variable has wrong value. If you use globals it is very hard to tell what is making this variables to hold wrong value as anywhere in code this variable can be changed using "global" – user3431635 Feb 22 '21 at 09:38