[Tutor] inheritance problem

Roelof Wobben rwobben at hotmail.com
Fri Oct 1 08:19:29 CEST 2010




----------------------------------------
> To: tutor at python.org
> From: alan.gauld at btinternet.com
> Date: Fri, 1 Oct 2010 00:59:06 +0100
> Subject: Re: [Tutor] inheritance problem
>
> "Roelof Wobben" 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 
>> 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?
 
I only see a pop here : card = self.pop() # take the top card
but no function called pop.
 

> So if the code you copied has an error how should it assign a card?

 
I assigned a card by this code :
 
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

 
 
> Where are the cards stored? How would you get the first card from
> the collection? Does that work?

The cards are stored in a directory named cards.
The hands are stored in a directory named hands
 
>
> 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.

 
The error is telling me that the function pop does not exist for a directory,
So solution is to find out which object contains pop and then change the code.
  		 	   		  


More information about the Tutor mailing list