O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Deck class

The Deck will need to contain 52 unique cards and must be able to shuffle itself. It will also need to be able to deal cards and decrease in size as cards are removed:

class Deck:    def __init__(self):        self.cards = [Card(s, v) for s in ["Spades", "Clubs", "Hearts",                      "Diamonds"] for v in ["A", "2", "3", "4", "5", "6",                       "7", "8", "9", "10", "J", "Q", "K"]]    def shuffle(self):        if len(self.cards) > 1:            random.shuffle(self.cards)    def deal(self):        if len(self.cards) > 1:            return self.cards.pop(0)

When creating an instance of the Deck, we simply need to have a collection of every possible card. We achieve this using a list comprehension which contains lists of every suit and value. We pass each combination over to the initialization for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required