I'm doing an exercise in "Core Python Programming" where you calculate the coins necessary to make an amount of money. This works most of the time:
but occasionally I get weird answers. 5.10 is five dollars, a nickel and four pennies. 4.56 is four dollars, a half dollar and a nickel. Weird
Python 2.5.4 on OS X 10.4.11
mt
Code:
coins = [0,0,0,0,0,0]
# dollars, half dollars, quarters, dimes, nickels, pennies
values = [100,50,25,10,5,1]
amountString = raw_input('Enter amount: ')
amount = 100*float(amountString)
for x in range(0,6,1):
coins[x] = amount//values[x]
amount %= values[x]
print "Dollars: ", coins[0]
print "Half Dollars: ", coins[1]
print "Quarters: ", coins[2]
print "Dimes: ", coins[3]
print "Nickels: ", coins[4]
print "Pennies: ", coins[5]
but occasionally I get weird answers. 5.10 is five dollars, a nickel and four pennies. 4.56 is four dollars, a half dollar and a nickel. Weird
Python 2.5.4 on OS X 10.4.11
mt