#!/usr/bin/env python # coding: utf-8 # # Card Shuffling Game # In[771]: from random import shuffle # In[772]: # define the cards suits = ["Hearts", "Diamonds", "Clubs", "Spades"] values = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'] # In[773]: class Card: def __init__(self, suit, value): self.suit = suit self.value = value # define each card def __repr__(self): return f"{self.value} of {self.suit}" # In[774]: class Deck: # all combinations of cards def __init__(self): self.cards = [Card(suit, value) for suit in suits for value in values] # count number of cards in a deck def count(self): return len(self.cards) # showing the count def __repr__(self): return f"Deck of {self.count()} cards" # the shuffle instance def shuffle(self): cards = self.cards if self.count() < len(self.cards): raise ValueError("Missing cards! Can't be shuffled") else: shuffle(cards) return cards # deal x number cards each time def _deal(self, number): count = self.count() if number > count: raise ValueError("No more cards to deal!") else: cards = self.cards[:number] self.cards = self.cards[number:] return cards # deal a single card each time def deal_card(self): cards = self._deal(1) return cards # deal a list of cards each time def deal_hand(self, number): cards = self._deal(number) return cards # # Checking the results # In[775]: # define each card card1 = Card("Hearts", "4") card1 # In[776]: # number of cards in a deck deck = Deck() deck # In[777]: # shuffle the deck deck.shuffle() # In[778]: # deal a single card deck.deal_card() # In[779]: # deal several cards deck.deal_hand(3) # In[780]: # deal more cards deck._deal(10) # In[781]: # only 38 cards left deck