# cards.py """ card.py a class that represents a playing card """ 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()] # while the two cards are the same, change one of 'em while self.cards[0].rank == self.cards[1].rank and self.cards[0].suit == self.cards[1].suit: self.cards[1] = 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()