Card
represents a playing card that has a face ('Ace'
, '2'
, '3'
, …, 'Jack'
, 'Queen'
, 'King'
) and a suit ('Hearts'
, 'Diamonds'
, 'Clubs'
, 'Spades'
)DeckOfCards
represents a deck of 52 playing cards as a list of Card
objectsDeckOfCards
¶Before we look at classes Card
and DeckOfCards
, let’s use an IPython session to demonstrate their capabilities.
from deck import DeckOfCards
deck_of_cards = DeckOfCards()
DeckOfCards
method __init__
creates the 52 Card
objects in order by suit and by face within each suitdeck_of_cards
object calls its __str__
method print(deck_of_cards)
deck_of_cards
object againdeck_of_cards.shuffle()
print(deck_of_cards)
Card
at a time by calling method deal_card
Card
object’s __repr__
method to produce the string output deck_of_cards.deal_card()
Card
’s Other Features¶str
functioncard = deck_of_cards.deal_card()
str(card)
Card
has a corresponding image file name, which you can get via the image_name
read-only propertyCard
s as imagescard.image_name
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.