Contents

  1. card3.py
  2. card4.py
  3. command_line_args.py

card3.py 1/3

[
top][prev][next]
class Card:
    """
    A class to represent a standard playing card. The ranks are ints:
    2-10 for numbered cards, 11=Jack, 12=Queen, 13=King, 14=Ace.
    The suits are strings: 'clubs', 'spades', 'hearts', 'diamonds'.
    """
    
    def __init__(self, rank, suit):
        "Constructs a new Card object with the given rank (an int) and suit (a string)."
        self.rank = rank
        self.suit = suit

    def __str__(self):
        "Returns a string describing the card as 'rank of suit'."
        result = ""
        if self.rank == 11:
            result += "Jack"
        elif self.rank == 12:
            result += "Queen"
        elif self.rank == 13:
            result += "King"
        elif self.rank == 14:
            result += "Ace"
        else:
            result += str(self.rank)
        result += " of " + self.suit
        return result

    def getRank(self):
        "Returns rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit

    def blackJackValue(self):
        "Returns the blackjack value of this card."
        if self.rank == 14:
            return 1
        elif self.rank > 10:
            return 10
        else:
            return self.rank

    def rummyValue(self):
        "Returns the value of this card in Rummy."
        if self.rank > 10 and self.rank <= 13:
            return 10
        elif self.rank == 14:
            return 15
        elif self.rank == 10:
            return 10
        else:
            return 5
    def __cmp__(self, other):
        """ Compares Card objects by their ranks """
        # Could compare by black jack value or rummy value
        if self.rank < other.rank:
            return -1
        if self.rank == other.rank:
            return 0
        return 1
        
        # Equivalent to : 
        #    return self.rank - other.rank 
        # or
        # return cmp(self.rank)
            
def main():
    c1 = Card(14, "spades")
    print c1
    print "Black Jack Value", c1.blackJackValue()
    
    c2 = Card(13, "hearts")
    print c2
    print "Black Jack Value", c2.blackJackValue()
       
    c3 = Card(3, "diamonds")
    print c3
    print "Rummy value", c3.rummyValue()
    
    # Demonstrate use of __cmp__ method
    print "\nDemonstrate use of __cmp__ method:"
    print c2, "less than", c3, "?", c2 < c3  # should be False
    print c3, "less than", c2, "?", c3 < c2  # should be True
    
    myCards = [c1, c2, c3]
    myCards.sort()
    
    print "\nSorted cards: "
    for card in myCards:
        print card
    
if __name__ == '__main__':
    main()

card4.py 2/3

[
top][prev][next]
class Card:
    """
    A class to represent a standard playing card. The ranks are ints:
    2-10 for numbered cards, 11=Jack, 12=Queen, 13=King, 14=Ace.
    The suits are strings: 'clubs', 'spades', 'hearts', 'diamonds'.
    """
    
    def __init__(self, rank, suit):
        "Constructs a new Card object with the given rank (an int) and suit (a string)."
        self.rank = rank
        self.suit = suit

    def __str__(self):
        "Returns a string describing the card as 'rank of suit'."
        result = ""
        if self.rank == 11:
            result += "Jack"
        elif self.rank == 12:
            result += "Queen"
        elif self.rank == 13:
            result += "King"
        elif self.rank == 14:
            result += "Ace"
        else:
            result += str(self.rank)
        result += " of " + self.suit
        return result

    def getRank(self):
        "Returns rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit

    def blackJackValue(self):
        "Returns the blackjack value of this card."
        if self.rank == 14:
            return 1
        elif self.rank > 10:
            return 10
        else:
            return self.rank
            
    def _isFaceCard(self):
        "Returns True iff this card is a face card (K, Q, or J)."
        if self.rank > 10 and self.rank < 14:
            return True
        return False

    def rummyValue(self):
        "Returns the value of this card in Rummy."
        if self._isFaceCard():
            return 10
        elif self.rank == 14:
            return 15
        elif self.rank == 10:
            return 10
        else:
            return 5
    
        
    def __cmp__(self, other):
        """ Compares Card objects by their ranks """
        # Could compare by black jack value or rummy value
        if self.rank < other.rank:
            return -1
        if self.rank == other.rank:
            return 0
        return 1
        
        # Equivalent to : 
        #    return self.rank - other.rank
        
            
def main():
    c1 = Card(14, "spades")
    print c1
    print "Black Jack Value", c1.blackJackValue()
    
    c2 = Card(13, "hearts")
    print c2
    print "Black Jack Value", c2.blackJackValue()
    
    #print c2._isFaceCard()
    
    c3 = Card(3, "diamonds")
    print c3
    print "Rummy value", c3.rummyValue()
    
    # Demonstrate use of __cmp__ method
    print "\nDemonstrate use of __cmp__ method:"
    print c2, "less than", c3, "?", c2 < c3  # should be False
    print c3, "less than", c2, "?", c3 < c2  # should be True
    
    myCards = [c1, c2, c3]
    myCards.sort()
    
    print "\nSorted cards: "
    for card in myCards:
        print card
    
if __name__ == '__main__':
    main()

command_line_args.py 3/3

[
top][prev][next]
# Demonstrate use of command-line arguments
# by Sara Sprenkle
# Usage: python command_line_args.py filename

import sys

PROG_ARG_LOC=0
FILENAME_ARG_LOC=1

# Make sure there are sufficient arguments.
if len(sys.argv) < 2:
    print "Error: invalid number of command-line arguments"
    print "Usage: python", sys.argv[PROG_ARG_LOC],"<filename>"
    sys.exit(1)
    
filename = sys.argv[FILENAME_ARG_LOC]
print "We will use file", filename, "as input"


Generated by GNU enscript 1.6.4.