[Tutor] Poor style to use list as "array"? CORRECTION

bob gailer bgailer at gmail.com
Mon Jul 6 15:04:05 CEST 2009


class Coin:

 def __init__(self, name, value, plural=None):
   self.name = name
   self.value = value
   if plural:
     self.plural = plural
   else:
     self.plural = self.name + 's'
   self.count = 0

 def display(self):
   if self.count == 0:
     return None
   if self.count == 1:
     return "%d %s" % (self.count, name)
   else:
     return "%d %s" % (self.count, self.plural)

#### next line unindented ###

coins = Coin('dollar', 100), Coin('quarter', 25), Coin('dime', 10), 
Coin('nickel', 5), Coin('penny', 1, 'pennies')
amnt = 99
buff = []
for coin in coins:
 (coin.count, amnt) = divmod(amnt, coin.value)
 d = coin.display()
 if d:
   buff.append(d)
if len(buff) < 2:
 print buff
else:
 print ', '.join(buff[:-1]) + " and " + buff[-1]

-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list