[Tutor] Attribute error message received on Card Game program

Dave Angel d at davea.name
Fri Feb 24 18:38:07 CET 2012


On 02/24/2012 11:34 AM, bob gailer wrote:
> On 2/24/2012 11:08 AM, SKHUMBUZO ZIKHALI wrote:
>> Hi
>> /*I am trying to run the following program from Guide to Programming 
>> with Python by Micheal Dawson:*/
>> /**/
>>
>> class Card(object):
>>     RANK = ["A","2","3","4","5","6","7"
>>             "8","9","K","Q","J"]
>>     SUIT = ["s","d","h","c"]
>>     def __init__(self,rank, suit):
>>         self.rank = rank
>>         self.suit = suit
>>     def __str__(self):
>>         rep = self.rank + self.suit
>>         return rep
>>
>> class Hand(object):
>>     def __init__(self):
>>         self.cards = []
>>     def __str__(self):
>>         if self.cards:
>>             rep = ''
>>             for card in self.card:
> Did you mean self.cards?
>>                 rep += str(Card)+''
>>         else:
>>             rep = "<empty>"

Did you mean to return   rep   for both the if and else portion?  If so, 
this return statement should be unindented to line up with the else: line.

>>             return rep
>>     def clear(self):
>>         self.cards =[]
>>     def add(self, card):
>>         self.cards.append(card)
>>     def give(self, card, other_hand):
>>         self.cards.remove(card)
>>         other_hand.add(card)
>> # Main
>> card1 = Card(rank = "A", suit ="c")
>> print 'Printing a Card object'
>> print card1
>> card2 = Card(rank = "2", suit ="c")
>> card3 = Card(rank = "3", suit ="c")
>> card4 = Card(rank = "4", suit ="c")
>> card5 = Card(rank = "5", suit ="c")
>> print card2
>> print card3
>> print card4
>> print card5
>> my_hand = Hand()
>> print '\nPrinting my hand before I add my card:'
>> print my_hand
>> my_hand.add(card1)
>> my_hand.add(card2)
>> my_hand.add(card3)
>> my_hand.add(card4)
>> my_hand.add(card5)
>> print '\nPrinting my hand after adding 5 cards:'
>> print my_hand
>> your_hand = Hand()
>> my_hand.give(card1, your_hand)
>> my_hand.give(card2, your_hand)
>> print '\nGave the first two cards from my hand to your hand.'
>> print 'Your hand:'
>> print your_hand
>> print 'My hand:'
>> my_hand
>> my_hand.clear()
>> print '\nMy hand after clearing it:'
>> raw_input('\n\nPress the enter key to exist.')
>> /*However, I can partly run it for eventually receive attribute error 
>> message. Printing response is as follows:*/
>> 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on 
>> win32
>> Type "copyright", "credits" or "license()" for more information.
>> >>> ================================ RESTART 
>> ================================
>> >>>
>> Printing a Card object
>> Ac
>> 2c
>> 3c
>> 4c
>> 5c
>> Printing my hand before I add my card:
>> <empty>
>> Printing my hand after adding 5 cards:
>> Traceback (most recent call last):
>>   File "C:/Python27/Card Game.py", line 69, in <module>
>>     print my_hand
>>   File "C:/Python27/Card Game.py", line 25, in __str__
>>     for card in self.card:
>> AttributeError: 'Hand' object has no attribute 'card'
>> >>>
>> /*Would you please help me as to where I got it wrong with this 
>> program and lastly inform how how does Hand object gets linked or 
>> connected with Card object? */
>> With thanks
>> Sikhumbuzo */
>> /*
>>


-- 

DaveA



More information about the Tutor mailing list