Hi Roelof,

> Im following this page : 
> http://openbookproject.net/thinkcs/python/english2e/ch17.html

I just checked this, and it appears you've copied this example fine.

<snip />

> class Deck:
>    def __init__(self):
>        self.cards = []
>        for suit in range(4):
>            for rank in range(1, 14):
>                self.cards.append(Card(suit, rank))
> 
>    def deal(self, hands, num_cards=999):
>        num_hands = len(hands)
>        for i in range(num_cards):
>            if self.is_empty(): break   # break if out of cards
>            card = self.pop()           # take the top card
>            hand = hands[i % num_hands] # whose turn is next?
>            hand.add(card)              # add the card to the hand

<snip />

> game = CardGame()
> hand = OldMaidHand("frank")
> deck = Deck()
> game.deck.deal([hand], 13)
> OldMaidHand.print_hands() 
> 
> But now Im getting this error message:
> 
> Traceback (most recent call last):
>  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 126, in 
> <module>
>    game.deck.deal([hand], 13)
>  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 24, in deal
>    card = self.pop()           # take the top card
> AttributeError: Deck instance has no attribute 'pop'
> 
> What went wrong here.

The error message tells you exactly what is wrong: there is no 'pop' attribute. 
Or in this case, rather there is no pop() method for the class Deck.
You're calling it as self.pop() within Deck, hence Deck should provide a pop 
method (the self refers to any instance of Deck that you're currently using). 
But pop() is nowhere defined...

Now, the commentary in the exercise says "a card is removed from the deck using 
the list method pop", which would suggest self is of type(list). Since self is 
also of type(Deck), Deck should inherit from list: class Deck(list).
Thus, in my view, the example is broken, and simply not well tested. Hence I 
said you copied it fine, and nothing went wrong there.

Seen the rest of the example, I actually suspect that inheriting from standard 
Python types is not yet discussed here, and will be done later. So inheriting 
from list is one option, but may not be right in the context of this exercise. 
Writing your own pop() method for Deck is another option.

And, since this is an openbookproject, I would expect that means you can edit 
it yourself. That doesn't appear to be directly the case, but you could at 
least notify the authors (bottom of page) and let them know that you think this 
example is broken (feel free to refer them to this thread in the mailing list). 
Unless of course, there was some example done before this chapter that provided 
a pop() method.

  Evert

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to