I tried out one of the "Projects for the Beginner" ideas: the change calculator. Basically, you tell it how much the purchased item was, and how much the customer paid. Then it tells you the change due, and the most efficient combination of bills and coins to pay it.
However, I'm getting some odd problems. Take this one, for example:
Cost of item: 499.99
Money paid: 500
Change: $0.00999999999999
100's: 0
50's: 0
20's: 0
10's: 0
5's: 0
1's: 0
Q's: 0
D's: 0
N's: 0
P's: 0
Merely a penny is due, but it seems to think that 500 - 499.99 is 0.0099999999? Another example has the same problem:
Cost of item: 8.98
Money paid: 9
Change: $0.02
100's: 0
50's: 0
20's: 0
10's: 0
5's: 0
1's: 0
Q's: 0
D's: 0
N's: 0
P's: 1
Can anyone help me fix this? Here is the source:
# change_calc.py
# dictionary storing the amount of each bill/coin needed
m_d = {100: 0, 50: 0, 20: 0, 10: 0, 5: 0, 1: 0, 0.25: 0, 0.1: 0, 0.05: 0, 0.01: 0}
# list of possible bills/coins, really only used in the for loop later on
m_l = [100, 50, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01]
cost = input("Cost of item: ")
paid = input("Money paid: ")
if paid < cost:
print "You have not paid enough"
else:
change = paid - cost
print "Change: $" + str(change)
for i in m_l:
while change >= i:
m_d[i] += 1
change -= i
print "100's:",m_d[100]
print " 50's:",m_d[50]
print " 20's:",m_d[20]
print " 10's:",m_d[10]
print " 5's:",m_d[5]
print " 1's:",m_d[1]
print " Q's:",m_d[0.25]
print " D's:",m_d[0.1]
print " N's:",m_d[0.05]
print " P's:",m_d[0.01]