Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
The first book in 2020 and some exercises.
  • Loading branch information
tweecho authored Jan 17, 2020
1 parent e94b598 commit ad2b96e
Show file tree
Hide file tree
Showing 3 changed files with 564 additions and 0 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Chapter 1
"""


"""P4 A Pythonic Card Deck"""
import collections
from random import choice

Card = collections.namedtuple('Card', ['rank', 'suit']) # Construct a simple class to represent individual cards

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]

def main():
deck = FrenchDeck()
length = len(deck)
print(length)

random_choice = choice(deck)
print(random_choice)

if __name__ == '__main__':
main()



Loading

0 comments on commit ad2b96e

Please sign in to comment.