Skip to content Skip to sidebar Skip to footer

How Do I Separate Each List

I'm very new to this and I don't know how to fix the problem. I have a Dealer and then it asks in the beginning how many players will be playing. Then each player as well as a Deal

Solution 1:

You probably want a list per player. A dictionary will probably do the work, something like:

hands = {}
...
for player in players:
    hand = hands[player] = []
    for j in range(2):
         c = deck.pop()
         hand.append(c)


for player in players:
    print('{}: {}'.format(player, ', '.join(hands[player])))

Solution 2:

You need to reset the hand for each player. So your second loop should look more like:

for i in range(players):
   hand = []
   for j in range(2):
      c = deck.pop()
      hand.append(c)
   print "Player " + str(i+1) + ":"
   print "Cards: " + cards.hand_display(cards)

Otherwise hand will continue to be appended to.

Solution 3:

your hand is a single array, so you are only appending the new cards to the same array, each time you print the hand, you are printing the same array, only at first it is with only 2 cards, then 4, then 6.

what you should do is use a dictionary, or an array of arrays.

Solution 4:

import random

class Cards(object):
    suit  = ['H', 'D', 'S', 'C']
    value = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

    def __init__(self):
        self.deck = [v+s for s in Cards.suit for v in Cards.value]

    def shuffle(self):
        random.shuffle(self.deck)

    def deal(self, n):
        res, self.deck = self.deck[:n], self.deck[n:]
        return res

show_hand = ' '.join

def main():
    num_players = 3
    deck = Cards()
    deck.shuffle()

    dealer  = deck.deal(2)
    players = [deck.deal(2) for i in xrange(num_players)]

    print('Hands:')
    print('  Dealer: {}'.format(show_hand(dealer)))
    for i in xrange(num_players):
        print('Player {}: {}'.format(i+1, show_hand(players[i])))

if __name__=="__main__":
    main()

results in

Hands:Dealer:JH9HPlayer 1:AS2DPlayer 2:QD8HPlayer 3:10H6D

Solution 5:

The problem with your code (which has now been removed) is that you were appending the cards for each player's hand to the same list. The simple fix would be to just reset the variable with at the top of the loop.

Since you likely will need to keep track of all the hands while playing the game, it would make sense to create a separate list of list containing one sub-list for the dealer's hand, plus a separate one for each player. These could be stored in a list of lists and index into as needed.

You're calling the Players: Player 1, Player 2, etc, in effect numbering them from 1, so it would be possible to store the dealer's hand in index 0, and put Player 1's cards in hands[player+1], and so on. Here's some sample code:

#### testing scaffold #######################import random

classCards(object):
    def__init__(self):
        deck = self._deck = []
        for rank in"A23456789JQK":
            for suit in"CDHS":
                deck.append(rank+suit)

    defdeck(self):
        return self._deck

    defhand_display(self, player_num):
        return' '.join(hands[player_num])

cards = Cards()
players = 3##########################################

deck = cards.deck()
random.shuffle(deck)
hands = [[]]  # for dealer's hand
hands.extend([] for player inrange(players))  # for each player's hand
DEALER = 0
CARDS_PER_HAND = 2print"Hands:"for i in xrange (CARDS_PER_HAND):
    cd = deck.pop()
    hands[DEALER].append(cd)
print"Dealer: " + cards.hand_display(DEALER)

for player_num in [player+1for player inrange(players)]:
    for j in xrange(CARDS_PER_HAND):
        c = deck.pop()
        hands[player_num].append(c)
    print"Player " + str(player_num) + ":"print"Cards: " + cards.hand_display(player_num)

Output:

Hands:
Dealer: 6H 5H
Player 1:
Cards: 7H 3H
Player 2:
Cards: 4D JD
Player 3:
Cards: 3S 4S

Post a Comment for "How Do I Separate Each List"