0

The problem is when counting the pennies, the pennies are sometime short 1 penny and change is equal to .01 at the end when it should be 0 once pennies are accounted for.

import os
clear = lambda: os.system('cls')

def dollar_counter(num):

    if num // 1 >= 1:
        dollars = num // 1
    else:
        pass
    return dollars

def quarter_counter(num):

    quarters = 0
    if num // .25 >= 1:
        quarters = num // .25
    else:
        pass
    return quarters

def dime_counter(num):

    dimes = 0
    if num // .10 >= 1:
        dimes = num // .10
    else:
        pass
    return dimes 

def nickel_counter(num):

    nickels = 0
    if num // .05 >= 1:
        nickels = num // .05
    else:
        pass
    return nickels

def penny_counter(num):

    pennies = 0
    if num // .01 >= 1:
        pennies = num // .01
    else:
       pass
    return pennies

#Run Program
while True:

    try:
        #get info from user
        payment = float(input("Enter what the customer paid: "))
        cost = float(input("Enter the cost: "))

        #round change to 2 decimals and output change due
        change = round(payment - cost,2)
        print("Change due: {}".format(change))

        #count dollars
        print(int(dollar_counter(change)), "dollars")
        change = change - float(dollar_counter(change))
        print(round(change,2)) #print the change each time for troubleshooting problems

        #count quarters
        print(int(quarter_counter(change)), "quarters")
        change = change - float(quarter_counter(change)*.25)
        print(round(change,2))

        #count dimes
        print(int(dime_counter(change)), "dimes")
        change = change - float(dime_counter(change)*.1)
        print(round(change,2))

        #count nickels
        print(int(nickel_counter(change)), "nickels")
        change = change - float(nickel_counter(change)*.05)
        print(round(change,2))

        #count pennies
        print(int(penny_counter(change)), "pennies")
        change = change - float(penny_counter(change)*.01) 
        print(round(change,2))

    except UnboundLocalError:
        print("Cost can be higher than amount paid. Try again.\n")
        continue
    else:
        replay = input("Are you done? Enter 'Yes' to continue or 'No' to quit: ")
        if replay[0].lower() == 'y':
            clear()
            continue
        else:
            break

I expect the change, after counting pennies, to be 0 but in most cases it ends with change == .01. Why is penny_counter() not accounting for the leftover change

parth
  • 23
  • 2
  • Side note: catching `UnboundLocalError` is not a good idea. It usually means you have written the code wrong. – rdas May 30 '19 at 03:24
  • Using floats for floor division is prone to rounding errors, take a look at [this thread](https://stackoverflow.com/questions/38588815/rounding-errors-in-python-floor-division). – jfaccioni May 30 '19 at 03:27

1 Answers1

0

In the function dollar_counter, you need to initialize dollars=0. As for the issue you're running into, I suggest printing out the variable num in penny_counter. You will see that it is not exactly 0.01, so after the division it will get rounded to 0. To fix this, I suggest either rounding the result before you pass it to the function, or convert the units to pennies for everything(so you can avoid floats entirely).

Untitled123
  • 1,317
  • 7
  • 20