[Chicago] Assistance on program failing to fully run

Tim Saylor tim.saylor at gmail.com
Fri Feb 24 17:10:34 CET 2012


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

These lines tell you all you need to know.  The green highlight tells you
what the problem is: it's looking for a "card" variable in the "Hand"
class, which doesn't exist.  The yellow highlight tells you where it
encountered this problem, line 25, and shows you what's on that line, your
for loop "for card in self.card".  As you can see from that it's just a
simple typo, the variable you added to the "Hand" class is "cards", not
"card".  If you change that it will fix your error.

On Fri, Feb 24, 2012 at 10:00 AM, SKHUMBUZO ZIKHALI <
akekhofananaye at yahoo.co.uk> 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:
>                 rep += str(Card)+''
>         else:
>             rep = "<empty>"
>             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  *
> *
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20120224/9c8bf73f/attachment.html>


More information about the Chicago mailing list