making change counter, cant divide coins
<p>I have to design a program that when an amount of an item is entered,  asks the user for the amount paid,and then provides change in 20's 10's 5's 1s quarters, dimes, nickels, and pennies. I've spent hours trying to find the flaws in my code but I can't get it right. If someone could help me out that would be greatly appreciated!</p>

<p>I realize that my code is not very polished, but I just started and haven't learned too many techniques so far.
I have it all working until it hits dimes, then things go bad.</p>

<p>Here is my code at this point:</p>

<pre><code>#assign variables#
import math
cost=float(input("Enter the price of the item: $"))
paid=float(input("Enter the amount paid: $"))
#calculations#
difference=round(paid,2)-round(cost,2)
change= round(difference,2)
#twentys#
remain20=float(change%20)
twent=float(change-remain20)
twent1=abs(float(twent)//20)
twent2=float(twent1*20)
sub1=float(change-float(twent2))
print(sub1,"sub1")
#tens

remain10=change%10
ten=sub1-remain10
ten1=ten//10
ten2=ten1*10
sub2=sub1-ten2
print(sub2,"sub2")
#fives

remain5=float(abs(change%5))
five=abs(float(sub2)-float(abs(remain5)))
five1=float(round(abs(five)//5))
five2=float(round(five*5))
sub3=abs(float(sub2)-abs(float(five2)))
print(sub3,"sub3")
#ones
remain1=change%1
one=abs(round(sub3)-abs(remain1))
one1=abs(round(one//1))
one2=abs(round(one*1))
sub4=abs(float(sub3))-abs(float(one2))
print(sub4,"sub4")
#quarters
remainq=change%(0.25)
remainq1=round(remainq,2)
q=abs(sub4)-(remainq1)
q1=abs(q//float(0.25))
q2=abs(q*0.25)
sub5=abs(float(sub4))-(float(q2))
print(sub5,"sub5")
#dimes

remaind=change%(0.10)
remaind1=round(remaind,2)
d=abs(round(sub5,2)-remaind1)
d1=abs(d//float(0.10))
d2=abs(d1*0.10)
sub6=abs(float(sub5))-abs(float(d2))
print(sub6,"sub6")
#nickles

remainn=change%(0.05)
remainn1=round(remainn,2)
n=abs(round(sub6,2)-abs(remainn1))
n1=abs(d//float(0.05))
n2=abs(d1*0.05)
sub7=float(abs(sub6)-float(n2))
print(sub7,"sub7")
#pennies

remainp=change%(0.01)
remainp1=round(remainp,2)
p=abs(round(sub7,2)-abs(remainp1))
p1=abs(d//float(0.01))
p2=abs(d1*0.01)
#outputs
print(round(twent1),str("Twenty dollar bills"))
print(round(ten1),str("Ten dollar bills"))
print(round(five1),str("Five dollar bills"))
print(round(one1),str("One dollar bills"))
print(round(q1),str("Quarters"))
print(int(d1),str("dimes"))
print(int(n1),str("nickles"))
print(int(p1),str("Pennies"))
</code></pre>

<p>Your primary problem is that monetary calculations (and remainder operations) don't work so well with <code>float</code>. Convert to an <code>int</code> number of pennies up front, and most sources of error disappear (as long as you round properly).</p>

<p>You can also simplify the code a lot. Right now, every denomination is its own special case, even though the work is fundamentally the same for all of them aside from the string name and the unit. Generalize the code, and you can simplify it all down to a simple loop with a couple (constant, top-level) <code>tuple</code>s defining the parts that actually change:</p>

<pre><code># These are the only thing that differ as you go, so just list them explicitly
denominations = ('Twenty dollar bills', 'Ten dollar bills', 'Five dollar bills', 'One dollar bills', 'quarters', 'dimes', 'nickels', 'pennies')
divisors = (2000, 1000, 500, 100, 25, 10, 5, 1)

def make_change(paid, cost):
    '''paid and cost should be floats with up to two decimal places of precision'''
    # Calculate the difference in price, then convert to an int penny count
    # so we can use accurate math
    change = round((paid - cost) * 100)  # Wrap round in int constructor on Py2

    # Not necessary if inputs are trusted, but good form
    if change &lt; 0: raise ValueError("Underpaid!")

    # Each loop replaces a special case from your code by swapping in the
    # string and divisor for that round
    for denom, div in zip(denominations, divisors):
        numdenom, change = divmod(change, div)
        # Conditional print; weird to say "0 fives", "0 ones" over and over
        # You can print unconditionally if you prefer
        if numdenom:
            print(numdenom, denom)
</code></pre>

<p>That's it. The whole of the calculation is (ignoring comments and blank lines) two constant definitions and seven lines of actual code.</p>

python