Hi there,
This is my first time posting so I am sorry if its not perfect

I am working on a FreeCell game right now where I need to deal a deck of cards into 7 separate piles. This is the code I have right now for dealing it into seven piles

import cards
deckDict = {}
mydeck = cards.Deck()
mydeck.shuffle()
x = 1
list_1 = []
list_2 = []
list_3 = []
list_4 = []
list_5 = []
list_6 = []
list_7 = []
while x <= 7:
    card = mydeck.deal()
    if card == None:
        break
    list_(int('%d' % (x))).append = card
    if x == 7:
        x = 1
    x += 1

This is the problem line

list_(int('%d' % (x))).append = card

I know I can do this with 'if' statements, but I would like to keep my code as short as possible and I have a feeling that you should be able to change the list, but I cannot find it on the web or figure it out myself.

Any help would be great, even if it is to tell me that I will just have to man up and use if statements.
Thanks, Andrew

I just noticed, it should be

list_(int('%d' % (x))).append(card)

but that doesn't fix the problem

Use a list of lists

card_list = [ [], [], [], [], [], [], [] ]
x = 0
while x < 7:     ## number of cards to be dealt to each hand
    ## each for() loop deals one card to each of the hands in card_list
    for ctr in range(len(card_list)):     ## = 7 (# hands in card_list)
        card = mydeck.deal()
        if card != None:
            card_list[ctr].append(card)
    x += 1  
for hand in card_list:
    print hand

or a dictionary which is similiar

card_dict = [1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[] ]
x = 0
while x < 7:
    for ctr in range(1, 8):
        card = mydeck.deal()
        if card != None:
            card_dict[ctr].append(card)
    x += 1
for ctr in range(1, 8):
    print card_dict[ctr]

Also note that you have an infinite loop as you reset "x" so it will never be greater than 7.

Edited 5 Years Ago by woooee: n/a

Comments
very clear explanation

thank you! and it isn't an infinite loop because the deck will run out and card will then = None which breaks the loop.

Then you just want to use
while True:
instead of messing with a counter since it has no meaning, or
for cards in range(52):
assuming there are no jokers (in the deck not in this forum). Also, you will have to post the code for "cards" if you have questions in the future and want us to be able to test the code.

Edited 5 Years Ago by woooee: n/a

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