Emile van Sebille emile@fenx.com
Thu, 17 Jun 1999 09:54:32 -0700

```Hi Kenneth,

I started with your code, and ended like this:

HTH - Emile

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from samplecubes import *
>>> test()
Item  Qty TtlVol
Ham    6    18
Egg    4     8
Spam    6    30
Total cube:  56
>>> test()
Item  Qty TtlVol
Ham    7    21
Egg    5    10
Spam    4    20
Total cube:  51
>>>

Source:

class Item:
def __init__(self, name, volume):
self.name = name
self.cube = volume

class Inven:
def __init__(self, limit):
self.contents = []
self.cubeLimit = limit
self.currentCube = 0
def insert(self, item):
if (item.cube + self.currentCube) > self.cubeLimit:
print item.name, "will not fit."
else:
self.currentCube = self.currentCube + item.cube
self.contents.append(item)
def remove(self, item):
self.currentCube = self.currentCube - self.item.cube
self.contents.remove(item)
def show(self):
print "Item\t\tQty\tTtlVol"
self.contents.sort()
xpos = 0
while xpos < len(self.contents):
x = self.contents[xpos]
testname, qty, ttlvol, xpos= x.name, 1, x.cube, xpos + 1
try:
while self.contents[xpos].name == testname:
x = self.contents[xpos]
qty, ttlvol, xpos = qty + 1, ttlvol + x.cube, xpos + 1
else:
print "%s\t\t% 3i\t% 5i" % (testname, qty, ttlvol)
except:
print "%s\t\t% 3i\t% 5i" % (testname, qty, ttlvol)
print "Total cube: ", self.currentCube

def test():
from whrandom import randint
box1 = Item('Ham', 3)
box2 = Item('Egg', 2)
box3 = Item('Spam', 5)
room1 = Inven(100)
for i in range(randint(2,7)):
room1.insert(box1)
for i in range(randint(2,7)):
room1.insert(box2)
for i in range(randint(2,7)):
room1.insert(box3)

room1.show()

Emile van Sebille
emile@fenx.com
-------------------

----- Original Message -----
From: Kenneth M. Power <teroc@zianet.com>
To: <tutor@python.org>
Sent: Wednesday, June 16, 1999 5:29 AM

> Here is my program:
>
> class Item:
>    def __init__(self, name, volume):
>       self.code = name
>       self.cube = volume
>
> class Inven:
>    def __init__(self, limit):
>       self.contents = []
>       self.cubeLimit = limit
>       self.currentCube = 0
>    def insert(self, item):
>       if (item.cube + self.currentCube) > self.cubeLimit:
>          print item.name, "will not fit."
>       else:
>          self.currentCube = self.currentCube + self.item.cube
>          self.contents.append(item)
>    def remove(self, item):
>       self.currentCube = self.currentCube - item.cube
>       self.contents.remove(item)
>    def show(self):
>       print "Item\t\tVolume"
>       for x in xrange(0, len(self.contents)):
>          print self.contents[x].name, "\t\t", self.contents[x].cube
>       print "Total cube: ", self.currentCube
>
> OK, now getting closer to where I want/need help or suggestions.
>
> first create an item and an inven object:
>
> box = Item('Box', 5)
> room1 = Inven(15)
>
> room1.insert(box)
> room1.show()
> Item        Volume
> Box        5
> Total Volume: 5
>
> room1.insert(box)
> room1.show()
> Item        Volume
> Box        5
> Box        5
> Total Volume: 10
>
> What I want is to accumulate like items into a group. Thus by putting
> two (2) boxes into my Inven(tory), I would get this result:
>
> room1.show()
> Item        Volume
> Box(2)    10 #Or something similar
> Total Volume: 10
>
> Any suggestions on how to accomplish this?
>
> Ken
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>
>

```