-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_card_pack.py
More file actions
34 lines (22 loc) · 877 Bytes
/
Python_card_pack.py
File metadata and controls
34 lines (22 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import collections
Card = collections.namedtuple('Card', ['rank', 'suit'])
# For more information about how namedtuple can be used, you can read
#https://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python
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]
beer_card = Card('7', 'diamonds')
print(beer_card)
print('This is how to use the FrenchDeck class')
deck = FrenchDeck()
print(len(deck))
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
print([Card(rank, suit) for suit in suits for rank in ranks])