Im trying to make a card class that I can use in other programs, the main goal of this is to make a deck of cards that would be valid, only 52 cards - no repeats, 4 suits - 13 cards each. My problem is I don't know how to work out the loops or any mechanism for testing what has already been drawn and then removing or only drawing different cards after that. Any help would be appreciated.

from random import *
from graphics import *

class DeckOfCards:
    def __init__(self):
        """Make a deck of cards with variables rank, suit and value"""
        self.rank = None
        self.suit = None
        self.value = None
    def getRank(self):
        return self.rank
    def getSuit(self):
        return self.suit

    def getValue(self):
        return self.value

    def RandomCard(self):
        """Test a card to see if its a valid draw"""
        self.PossSuit = ["h","s","d","c"]

        while cardCount<53:
            while self.PossCards:
                self.value = randrange(0,len(self.PossCards))+1
                self.rank = self.PossCards[self.value-1]

                for x in range(len(rankCount)):
                    if rankCount[x]>4:
                    while self.PossSuit:
                        self.suit = self.PossSuit[randrange(0,len(self.PossSuit))]
                        if self.suit=="h":
                            if hearts>12:
                        elif self.suit=="s":
                        elif self.suit=="d":
                            if diamonds>12:
                            if clubs>12:

I would do something like:

import random
from graphics import *

class DeckOfCards(object):
    def __init__(self, *cards):
        """Make a deck of cards with variable suit and value"""
        if not cards:
            self.deck = cards
    def deal(self, num=1):
        for count in range(num):
            yield self.deck.pop(0)
    def make_deck(self):
        """Prepare deck"""
        self.suits = ["h","s","d","c"]

        self.deck = [(value, suit)
                     for value in self.values
                     for suit in self.suits]
    def __repr__(self):
        return 'DeckOfCards(%s)' % (','.join(repr(card) for card in self.deck))

    def __len__(self):
        return len(self.deck)

    def __iter__(self):
        return iter(self.deck)

    def __getitem__(self, n):
        return self.deck[n]

mydeck = DeckOfCards()
print 'Your five cards are:\n ',tuple(
print 'There are %i cards in deck now:\n%s' % (len(mydeck), mydeck)

print 'In other way the cards are:'
for card in mydeck:
    print card

print 'Next card dealt will be:', mydeck[0]

Edited 4 Years Ago by pyTony

Here's Python 3.x code I wrote for cards and decks. Maybe it will give you some inspiration.

In most implementations, suits and ranks are simply represented by numbers. I make my implementation only slightly more complex for extra expressiveness. (My suits have symbols and words, and my ranks have words.)

I do a lot of early computation in some __init__ methods, this is usually a bad idea if you create a lot of objects and don't need the data immediately. However, my card and deck objects get created only once. Each new deck is a copy.

On my machine I can create a deck, shuffle it, and draw 7 cards 500,000 times in under a minute. (I have a top-tier CPU though.)

Edited 5 Years Ago by lrh9: n/a

This article has been dead for over six months. Start a new discussion instead.