Python code not working - confusion about if statement -


i'm writing simple code calculate how change due, print statements thrown in check values.

cost = float(raw_input('how did meal cost? > ')) payment = float(raw_input('how did pay? > '))  change = (payment - cost)  print change  if change < 0:     print 'you need pay $%.2f more!' %  (-change)  dollars, q, d, n, p = 0, 0, 0, 0, 0  change = float(change*100)  print change  if change> 100:     dollars = int(change/100)     change = change % 100     print change  if change >= 25:     q = int(change/25)     change = change % 25     print change  if change >= 10:     d = int(change/10)     change = change % 10     print change  if change >= 5:     n = int(change/5)     change = change % 5     print change      if change >= 1:     p = int(change/1) else:     print "why doesn't work?"     print p     print change  print 'you %d dollars, %d quarters, %d dimes, %d nickels, , %d pennies!' % (dollars, q, d, n, p) 

this returns:

how did meal cost? > 34.34

how did pay? > 40

5.66

566.0

66.0

16.0

6.0

1.0

why doesn't work?

0

1.0

i can see change = 1.0 before , after pennies calculation, but

if change >= 1 

still reads false, , goes else statement. what's happening here , how can fix it?

it's ignacio says. easy fix throw in line round change second decimal place. using function simplify things little bit, code becomes:

def get_change_and_coin(coin_amount, change):     change = round(change, 2)     return (change % coin_amount, int(change / coin_amount))   cost = float(raw_input('how did meal cost? > ')) payment = float(raw_input('how did pay? > '))  change = (payment - cost)  print change  if change < 0:     print 'you need pay $%.2f more!' %  (-change)  dollars, q, d, n, p = 0, 0, 0, 0, 0  dollars = int(change) change = float(change*100)  (change, q) = get_change_and_coin(25, change) (change, d) = get_change_and_coin(10, change) (change, n) = get_change_and_coin(5, change) (change, p) = get_change_and_coin(1, change)  print 'you %d dollars, %d quarters, %d dimes, %d nickels, , %d pennies!' % (dollars, q, d, n, p) 

hope helps!


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -