Not logged in. Login

A3 Cards Module Docs

Download the cards module. Save this file in the same directory as your blackjack.py file and you should be able to import cards.

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

A 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: c = Card('8', 'C')

The card's suit and rank cannot be changed after it is instantiated.

Properties

There are no public properties of a card object.

Methods

Card.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'

Card.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'

Card.shortname(): Return a short two-character description of the card. This is the string that is printed if you print a Card object.

>>> c = Card('8', 'C')
>>> c.shortname()
'8C'

Card.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
Updated Thu June 27 2024, 09:55 by ggbaker.