This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.
You can download
cards.pyc
for Python 2.5,
cards.pyc
for Python 2.6,
cards.pyc
for Python 2.7.
Constants
suits
-
The possible values for a card's suit,
['S', 'C', 'D', 'H']
. ranks
-
The possible values for a card's rank,
['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
. Note that 10 is represented by'T'
.
The Card
class
Class to hold information about a single playing card. The card's rank and suit are stored.
The constructor takes two arguments, the rank and suit of the card. The
rank and suit must be values from the ranks
and suits
lists. This creates an eight of clubs:
The card's suit and rank cannot be changed after it is instantiated.
Properties
There are no public properties of a card
object.
Methods
suit()
-
Return a character representing the card's suit. This will be one of the characters from
suits
.>>> c = Card('8', 'C')
>>> c.suit()
'C' rank()
-
Return a character with the card's rank. This will be one of the characters from
ranks
.>>> c = Card('8', 'C')
>>> c.rank()
'8' shortname()
-
Output a short two-character description of the card. This is the string that is printed if you
print
aCard
object.>>> c = Card('8', 'C')
>>> c.shortname()
'8C' longname()
-
Return a long English description of the card.
>>> c = Card('8', 'C')
>>> c.longname()
'eight of clubs'
Functions
These functions can be used in your program to do some of the work. They are designed to work on “hands” of cards. A “hand” is a list of Card
objects.
hand_display(hand)
-
Create a string that represents the cards in the player's hand. This can be used when the program needs to report the contents of a player's hand and for testing.
>>> testhand = [ Card('6', 'C'), Card('6', 'D'), Card('7', 'S'), Card('9', 'H'), Card('A', 'H') ]
>>> hand_display(testhand)
'6C 6D 7S 9H AH' deck()
-
Return an unshuffled deck of cards (list of card objects).
>>> d = deck()
>>> print hand_display(d)
2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AS 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AC 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AD 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AH
>>> print len(d)
52