Chapter 15 of How to Think Like a Computer Scientist
teaches you how to deal with sets of objects by
creating a deck of cards.  The tutorial can be found
here: http://www.ibiblio.org/obp/thinkCSpy/chap15.htm.

Here is my code so far in card.py:

class Card:
        suitList = ["Clubs", "Diamonds", "Hearts", "Spades"]
        rankList = ["narf", "Ace", "2", "3", "4", "5", "6",
"7",
                     "8", "9", "10", "Jack", "Queen", "King"]

        def __init__(self, suit=0, rank=0):
                self.suit = suit
                self.rank = rank
                
        def __str__(self):
                return (self.rankList[self.rank] + " of " +
                        self.suitList[self.suit])
                        
        def __cmp__(self):
                #check the suits
                if self.suit > other.suit: return 1
                if self.suit < other.suit: return -1
                #suits are the same...check ranks
                if self.rank > other.rank: return 1
                if self.rank < other.rank: return -1
                #ranks are the same...it's a tie
                return 0
                
                
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 __str__(self):
                s = ""
                for i in range(len(self.cards)):
                        s = s + " "*i + str(self.cards[i]) + "\n"
                return s
                
        def shuffle(self):
                import random
                random.shuffle(self.cards)
                
        def removeCard(self, card):
                if card in self.cards:
                        self.cards.remove(card)
                        return 1
                else:
                        return 0
                        
        def popCard(self):
                return self.cards.pop()
                
        def isEmpty(self):
                return (len(self.cards) == 0)
                
I get an error message when I use the removeCard
function:

>>> from card import *
>>> deck = Deck()
>>> print deck
Ace of Clubs
 2 of Clubs
  3 of Clubs
   4 of Clubs
        etc.
>>> deck.removeCard(Card(0,2))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "card.py", line 43, in removeCard
    if card in self.cards:
TypeError: __cmp__() takes exactly 1 argument (2
given)

I am confused by this error because I modified __cmp__
in the Card class to take two arguments.  Am I missing
something here?


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to