[Tutor] Cards Program
Rob Andrews
randrews@planhouse.com
Wed, 21 Feb 2001 10:32:09 -0600
How about adding a little extra flow control during the card dealing so that
a list of players is iterated through and dealt to w/o having to restart the
deal with a fresh deck?
One idea is to try coding this program using classes, having a *dealer* and
several *player* objects.
Rob
----- Original Message -----
From: "Timothy M. Brauch" <tbrauch@mindless.com>
To: "Python Tutor" <tutor@python.org>
Sent: Tuesday, February 20, 2001 10:28 PM
Subject: [Tutor] Cards Program
> 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
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor