current position:Home>Some discussions on playing cards -- Reading Notes of fluent Python 2

Some discussions on playing cards -- Reading Notes of fluent Python 2

2022-05-15 06:25:04Li ziti

High quality resource sharing

Learning route guidance ( Click unlock ) Knowledge orientation Crowd positioning
🧡 Python Actual wechat ordering applet 🧡 Progressive class This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system .
Python Quantitative trading practice beginner Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system

One 、 explain

The reference is Wikipedia's Playing Card entry , Non serious argument , Just discuss why the code is written like this .

Two 、 The origin of playing cards

import collections

Card = collections.namedtuple('Card', ['rank', 'suit'])


class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = 'spades diamonds clubs hearts'.split()

    def \_\_init\_\_(self):
        self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]

    def \_\_len\_\_(self):
        return len(self._cards)
    
    def \_\_getitem\_\_(self, position):
        return self._cards[position]

Class called “FrenchDeck”—— When reading, I wonder why I use “French” Well , Is poker invented by the French ? According to Wikipedia : The most common type of playing card is that found in the French-suited,standard 52-card deck. That is, the most common card type is the French standard 52 Cards ( notes : The code doesn't include “ Ghost card ”).

3、 ... and 、 Decor order

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]

The decor order is :spades( Spade ) > hearts( Heart ) > diamonds( square ) > clubs( The plum blossom )( notes : You can remember in alphabetical order :s > h > d > c).( notes : The name and order of designs and colors are slightly different in different places , In my own hometown is :spades( Big flower ) > hearts( Flatter ) > clubs( The plum blossom ) > diamonds( Sharp liver ))

Four 、beer card

>>> beer_card = Card('7', 'diamonds')
>>> beer_card
Card(rank='7', suit='diamonds')

notice “beer_card” When , Also curious about why “beer” Well ? Refer to Wikipedia Beer card entry ,beer card It's the right square 7 Informal address for , As for how to play the game , I don't quite understand , I won't repeat it here .

copyright notice
author[Li ziti],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/135/202205142251590358.html

Random recommended