Random Module help

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Feb 11 00:42:08 EST 2002


----- Original Message ----- 
From: "Patio87" <patio87 at aol.com>


> 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

Hmm.  First, I suppose you want your cards in a list:

    deck = [ (11, "Ace of Spades"),
             (10, "King of Spades"),

and so on.  Note that I am representing the cards as tuples of 
(value, name) where of course you will have to deal with the dual
values of the Aces in your code.

In this case, you might use random.shuffle() to shuffle the
deck:

    random.shuffle(deck)

although if you want to play multiple games, you will need to play
from a *copy* of the deck:

    current_deck =  master_deck[:]

    random.shuffle(current_deck)

something like that.  Draw cards using the .pop() method:

    card = current_deck.pop()

This puts a card tuple into the 'card' variable and removes it from
the current_deck.

Have fun.






More information about the Python-list mailing list