Random Module help

Jonathan Gardner jgardn at alumni.washington.edu
Mon Feb 11 08:30:36 CET 2002

Patio87 wrote:

> I was wondering if any 1 could tell me how to use the random module for a
> blackjack game i want to make. I am a begginer so try to be easy with the
> dialouge. Thanks

You'd want a card class first of all that represents a card. I think 
something like this would be sufficient:

class card:
        def __init__(self, number, suit):
                self.number = number
                self.suit = suit

        def __str__(self):
                return "%s %s" % (self.number, self.suit)

To represent a group of cards, you can just use an array. So, to define the 
original stack of 52 cards, you'd do something like:

deck = []
for suit in ('heart', 'spade', 'diamond', 'club'):
        for number in (
                '2', '3', '4', '5', '6', 
                '7', '8', '9', '10', 'J', 
                'Q', 'K', 'A' ):
                deck.append(number, suit)

Now you want to shuffle the cards up. There are several ways to shuffle the 
cards. The way that I think is best is to use random.shuffle() because it 
is easy.

import random

Now you have a shuffled deck of cards.

Good luck - the devil is in the details, of course.


More information about the Python-list mailing list