Thursday, July 30, 2015

Deck class - 7/30

Here is the code for the Deck class and Card class that we wrote today during class:

import random

class Card:
    def __init__(self, card_suit, card_number):
        self.suit = card_suit
        self.number = card_number

class Deck:
    def __init__(self):
        self.deck = []
        for x in range(1, 14):
            self.deck.append(Card("hearts", x))
            self.deck.append(Card("diamonds", x))
            self.deck.append(Card("spades", x))
            self.deck.append(Card("clubs", x))
        self.shuffle()
    def draw_card(self):
        x = self.deck.pop(0)
        return x
    def shuffle(self):
        random.shuffle(self.deck)
       
myDeck = Deck()
for x in myDeck.deck:
    print str(x.number) + " of " + x.suit

No comments:

Post a Comment