fluentpython / example-code-2e

Example code for Fluent Python, 2nd edition (O'Reilly 2022)
https://amzn.to/3J48u2J
MIT License
3.26k stars 919 forks source link

More consistent way to get suit value and suits length #22

Open ricefryegg opened 2 years ago

ricefryegg commented 2 years ago

Change from using different way to get suit value than rank_value

suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)

def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)
    return rank_value * len(suit_values) + suit_values[card.suit]

To be more consistent. Also we can get suit_values from existing class variable FrenchDeck.suits.

def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)
    suit_value = FrenchDeck.suits.index(card.suit)
    return rank_value * len(FrenchDeck.suits) + suit_value
Florimond commented 2 years ago

Also, I may need glasses, but in the listing page 5, the order of the suits is not the same as the order in suit_values on page 7.