[Tutor] inheritance problem
Alan Gauld
alan.gauld at btinternet.com
Fri Oct 1 01:59:06 CEST 2010
"Roelof Wobben" <rwobben at hotmail.com> wrote
> So i have this programm now :
> 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
>
> def shuffle(self):
> import random
> num_cards = len(self.cards)
> for i in range(num_cards):
> j = random.randrange(i, num_cards)
> self.cards[i], self.cards[j] = self.cards[j],
> self.cards[i]
>
> def remove(self, card):
> if card in self.cards:
> self.cards.remove(card)
> return True
> else:
> return False
> def is_empty(self):
> return (len(self.cards) == 0)
> 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 tells you what is wrong, the Deck has no attribute called
pop.
Can you see a pop anywhere?
So if the code you copied has an error how should it assign a card?
Where are the cards stored? How would you get the first card from
the collection? Does that work?
Think about what the error is telling you and think about how you
would fix it. If you don't understand what the error is saying then
tell
us and we can explain it, but in this case its pretty clearly stated
and is one you have seen before.
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list