📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division1213from Card import Hand, Deck141516class PokerHand(Hand):17"""Represents a poker hand."""1819def suit_hist(self):20"""Builds a histogram of the suits that appear in the hand.2122Stores the result in attribute suits.23"""24self.suits = {}25for card in self.cards:26self.suits[card.suit] = self.suits.get(card.suit, 0) + 12728def has_flush(self):29"""Returns True if the hand has a flush, False otherwise.3031Note that this works correctly for hands with more than 5 cards.32"""33self.suit_hist()34for val in self.suits.values():35if val >= 5:36return True37return False383940if __name__ == '__main__':41# make a deck42deck = Deck()43deck.shuffle()4445# deal the cards and classify the hands46for i in range(7):47hand = PokerHand()48deck.move_cards(hand, 7)49hand.sort()50print(hand)51print(hand.has_flush())52print('')53545556