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 Hand class

A Hand class will need to contain cards just like the Deck class does. It will also be assigned a value by the rules of the game based on which cards it contains.

Since the dealer's hand should only display one card, we also keep track of whether the Hand belongs to the dealer to accommodate this rule: 

class Hand:    def __init__(self, dealer=False):        self.dealer = dealer        self.cards = []        self.value = 0    def add_card(self, card):        self.cards.append(card)

Much like the Deck, a Hand will hold its cards as a list of Card instances.

When adding a card to the hand, we simply add the Card instance to our cards list.

Calculating the value of a Hand is where the rules of the game come into play the most:

def calculate_value(self): self.value ...

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