Classes in modules

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Apr 4 15:57:49 EDT 2008


En Thu, 03 Apr 2008 18:00:54 -0300, <hexusnexus at gmail.com> escribió:

> Yeah, I was a little embarrassed putting my code up to be examined.
> Thanks for the reply.  I typed up some classes, but I seemed to have
> run into some more problems.  One of the classes keeps getting an
> error that it can't pop from an empty list.  Here's the code so far:
>
> #deck functions
> class Deck(object):
>     def __init__(self):
>         self.deck = []
>         for i in range(52):
>             self.deck.append(Cards(i % 13, i / 13))
>     def draw(self):             #This is where my (first) problem
> arises, though there may be more
>         return self.deck.pop()  #IndexError: pop from empty list
>     def size(self):
>         return len(self.deck)

Next time post the whole traceback including the exception message. It  
really contains valuable information. Without it, one has to guess...
"pop from empty list" means that you are trying to draw a card after the  
deck is empty. The traceback would show the caller, but in this case it's  
easy because there is a single place:

> class Game(object):  #the main game class
>     def __init__(self):
>         self.deck = Deck()
>         self.deck.shuffle()
>         self.player = Hand()
>         self.computer = Hand()
>         for i in range(self.deck.size()):
>             self.player.put(self.deck.draw())
>             self.computer.put(self.deck.draw())
>         self.stock = Stock()
>         print self.stock.showtrump()

The loop takes 52 iterations but you draw two cards in each iteration -  
the 27th sees an empty deck.

-- 
Gabriel Genellina




More information about the Python-list mailing list