创建了一个简单的类FrenchDeck
import collections
Card =collections.namedtuple('Card',['rank','suit'])
class FrenchDeck:
ranks =[str(n) for n in list(range(2,11))+list('JQKA')] #python3中需要使用list来定义该数据
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, item):
return self._cards[item]
实现类中的方法
from mine.__init__ import *
from random import choice
beer_Card = Card('7','diamonds')
print(beer_Card)
deck =FrenchDeck()
print(len(deck)) 》》》52
print(deck[0])
print(deck[-1])
print(deck[:3])
print(deck[12::13])
print(choice(deck))
for card in deck: #doctest:+ELLIPSIS
print(card) #正向迭代
# print(c)
for card in reversed(deck): #反向迭代
print(card)
print(Card('Q','hearts') in deck)
print(Card('7','beasts') in deck) #beasts 野兽
suit_values = dict(spades =3,hearts =2 ,diamonds=1,clubs=0)