[Tutor] inheritance problem

Alan Gauld alan.gauld at btinternet.com
Fri Oct 1 09:29:33 CEST 2010


"Roelof Wobben" <rwobben at hotmail.com> wrote

>>> 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
>>

No this code assigns several HANDS not just cards.
The code that assigns a card is inside this function, but
you need to narrow it down to the specific lines assigning the card.

>>> 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.

Correct. So how do you get a card from the collection stored
in self.cards?

>> 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.

Correct

> I make a big mistake.
> Both card and hands are lists and pop can be used on a list.

That's not a mistake, lists are objects too.

> So as Joel said in this rule card = self.pop() there is no list
> named so I think it must be self.hands.pop()

If you want to assign a card and cards are stored in self.cards
why would you use self.hands.pop()?
Surely it should be self.cards.pop()?

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list