[Tutor] Cards Program

Timothy M. Brauch tbrauch@mindless.com
Tue, 20 Feb 2001 23:28:14 -0500


I am slowly working on a program that simulates dealing cards.  While
right now I have no real purpose for this program, it is just something
I thought I'd like to try.  However, I have come to a stumbling block. 
As it is right now, I can get the program to deal as many cards as I
want, but I cannot think of a way to get the cards into seperate hands,
other than just having the program deal out the cards and then manually
split these cards up into hands myself.  That is not what I am looking
for.  Here is my code so far:

######################################################################

import random

cards = input('How many cards do you want for each hand? ')
dealt=[]
c=0
values=['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
suits=['Hearts','Spades','Diamonds','Clubs']

def card():
    value=random.randint(0,12)
    suit=random.randint(0,3)
    return [suit,value]
    
while (c<cards):
    new_card=card()
    if new_card not in dealt:
        dealt.append(new_card)
        c=c+1
    else: pass

dealt.sort()
for item in dealt:
    print values[item[1]]+' of '+suits[item[0]]

raw_input('Press enter to end. ')

######################################################################

This code will work for one hand, but if I try to just run it again for
anotehr hand, there will be repeated cards.  Ideally I'd like to be able
to have the program ask me how many hands I want dealt and how many
cards in each hand.  But I just cannot seem to make anything I try
work.  Any help would be appreciated.

 - Tim