> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> On Behalf Of Ed Leafe
> Sent: Monday, August 13, 2007 12:26 PM
> To: Dabo Users list
> Subject: Re: [dabo-users] IIF()
>
> On Aug 13, 2007, at 3:13 PM, Jeff Johnson wrote:
>
> > Ed: "for pos, denom in denominations:" gave me a "type error -
> > unpacking
> > non-sequence" so I changed it to:
> >
> > "for pos in range(len(denominations)):" and it worked.
> >
> > I am using Python 2.5.
>
> My bad: I wrote it one way to test, and then re-wrote it from memory
> into the email (with real var names instead of x, y and such).
>
> The missing thing is the enumerate() function. It returns a 2-tuple:
> the iteration (starting with zero), and the item in the list. So
> change the code to:
>
> for pos, denom in enumerate(denominations):
>
> The enumerate function is very useful, as it eliminates the need for
> managing a counter variable.
>
> -- Ed Leafe
> -- http://leafe.com
> -- http://dabodev.com
>
Ed: Thanks! I learned a lot from this little exercise.
cents = 100
cost = int(round(cents * float(raw_input("Enter the cost: "))))
tendered = int(round(cents * float(raw_input("Enter the amount tendered:
"))))
change = tendered - cost
denominations = [2000, 1000, 500, 100, 50, 25, 10, 5, 1]
money = ['$20.00', '$10.00', '$5.00', '$1.00', 'Fifty Cent Piece',
'Quarter', 'Dime', 'Nickel', 'Penny']
moneys = ['$20.00', '$10.00', '$5.00', '$1.00', 'Fifty Cent Pieces',
'Quarters', 'Dimes', 'Nickels', 'Pennies']
givetocustomer = [0, 0, 0, 0, 0, 0, 0, 0, 0]
for pos, denom in enumerate(denominations):
givetocustomer[pos], change = divmod(change, denom)
if givetocustomer[pos] > 0:
if givetocustomer[pos] > 1:
print "%s - %s" % (givetocustomer[pos], moneys[pos])
else:
print "%s - %s" % ( givetocustomer[pos], money[pos])
Jeff
Jeff Johnson
[EMAIL PROTECTED]
623-582-0323
Fax 623-869-0675
--- StripMime Report -- processed MIME parts ---
multipart/signed
text/plain (text body -- kept)
application/x-pkcs7-signature
---
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]