Question: Python # Deck.py from random import randrange from Card import Card class Deck(object): #------------------------------------------------------------ def __init__(self): post: Creates a 52 card deck in standard order

Python

# Deck.py

from random import randrange from Card import Card class Deck(object):

#------------------------------------------------------------

def __init__(self):

"""post: Creates a 52 card deck in standard order""" list cards = [] for suit in Card.SUITS: for rank in Card.RANKS: cards.append(Card(rank,suit)) self.cards = cards

#------------------------------------------------------------

def size(self):

"""Cards left post: Returns the number of cards in self"""

return len(self.cards)

#------------------------------------------------------------

def deal(self):

"""Deal a single card pre: self.size() > 0 post: Returns the next card, and removes it from self.card if the deck is not empty, otherwise returns False"""

if self.size() > 0: return self.cards.pop() else: return False

#------------------------------------------------------------

def shuffle(self):

"""Shuffles the deck post: randomizes the order of cards in self"""

n = self.size() cards = self.cards for i,card in enumerate(cards): pos = randrange(i,n) cards[i] = cards[pos] cards[pos] = card

Python # Deck.py from random import randrange from Card import Card class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!