Forget floating point, that way lies madness if you don't understand what's going on under the covers.
If you want exactness, turn the value into an integer as soon as possible:
money = float(raw_input("Input the money: "))
money = int (money * 100 + 0.1) # Make integral pennies
# avoiding 0.999999 problems
twofive_count = 0
ten_count = 0
five_count = 0
one_count = 0
while money > 0:
if money >= 25: # Make sure you use pennies
money -= 25
twofive_count +=1
elif money >= 10:
money -= 10
ten_count += 1
elif money >= 5:
money -= 5
five_count += 1
elif money >= 1:
money -= 1
one_count += 1
print "Quarters:",twofive_count
print "Dimes:",ten_count
print "Nickels:",five_count
print "Pennies:",one_count
total_change = twofive_count + ten_count + five_count + one_count
print "Total number of coins in change:",total_change
You'll also notice I've cleaned up your coin detection code. There is no need to check both ends of the range (e.g., less than a nickel and at least a penny) because you're using elif
which means the first part of that condition has already been checked.
Of course, there's often a more efficient way to do it if you think about it a bit. A first step would be breaking each step into its own while
to isolate each coin count calculation, something like:
while money >= 25:
money -= 25
twofive_count +=1
while money >= 10:
money -= 10
ten_count += 1
while money >= 5:
money -= 5
five_count += 1
while money >= 1:
money -= 1
one_count += 1
From there, it's a short step to realising that, once you've worked out all non-penny values, the number of pennies can be done quicker:
while money >= 25:
money -= 25
twofive_count +=1
while money >= 10:
money -= 10
ten_count += 1
while money >= 5:
money -= 5
five_count += 1
one_count = money
But there's also a way to figure out each coin count without repeated subtraction, since repeated subtraction is exactly why division was invented :-)
twofive_count = int (money / 25)
money -= (twofive_count * 25)
ten_count = int (money / 10)
money -= (ten_count * 10)
five_count = int (money / 5)
money -= (five_count * 5)
one_count = money