April 2018
Beginner
340 pages
7h 54m
English
The Deck will need to contain 52 unique cards and must be able to shuffle itself. It will also need to be able to deal cards and decrease in size as cards are removed:
class Deck: def __init__(self): self.cards = [Card(s, v) for s in ["Spades", "Clubs", "Hearts", "Diamonds"] for v in ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]] def shuffle(self): if len(self.cards) > 1: random.shuffle(self.cards) def deal(self): if len(self.cards) > 1: return self.cards.pop(0)
When creating an instance of the Deck, we simply need to have a collection of every possible card. We achieve this using a list comprehension which contains lists of every suit and value. We pass each combination over to the initialization for ...