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