""" blackjack_pair.py An example of : * two classes, Card and PairOfCards * the python dictionary * the __str__ and __cmp__ special methods Things that could be better : * add docstring tests * add Hand class that can include more than two cards * add Deck class that cards are pulled from to avoid duplicates Jim Mahoney | Nov 2012 """ import random class Card: """ This class creates the cards, their ranks, and their suits. """ ranks = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"] suits = {'s':"Spades", 'c':"Clubs", 'd':"Diamonds", 'h':"Hearts"} def __init__(self, rank=False, suit=False): if not suit: suit = random.choice(['s', 'd', 'h', 'c']) if not rank: rank = random.randint(1, 13) self.rank = rank self.suit = suit def __str__(self) : return self.getRank() + " of " + self.getSuit() def getRank(self): """ return i.e. "Two" if rank is 2 """ return Card.ranks[self.rank - 1] def getSuit(self): """ return i.e. "Diamonds" if suit is 'd' """ return Card.suits[self.suit] def blackjackValue(self): if self.rank > 10: bjvalue = 10 else: bjvalue = self.rank return bjvalue class PairOfCards: """ two cards, including comparing their blackjack sum """ def __init__(self): self.cards = [Card(), Card()] def __str__(self): return str(self.cards[0]) + ' and ' + str(self.cards[1]) def pair_value(self): return self.cards[0].blackjackValue() + self.cards[1].blackjackValue() def __cmp__(self, other): return cmp(self.pair_value(), other.pair_value()) def main(): (c1, c2) = (Card(), Card()) print "The first card is " + str(c1) print "The second card is " + str(c2) print "The blackjacksum is " + \ str(c1.blackjackValue() + c2.blackjackValue()) my_pair = PairOfCards() your_pair = PairOfCard() if my_pair > your_pair: print "your blackjack sum is higher" main()