3

I have made the following code in Python. Where deck = deck_of_cards(). My question is how to I fix the second function so that it must deal a first card to each player before dealing a second card. The way it is right now seems to deal two cards to a player before dealing cards to the other players and I don't know how to fix it.

import random
def deck_of_cards():
    suits = ['D', 'C', 'H', 'S']
    denominations = ['2', '3', '4', '5', '6', '7', '8', 
                     '9', 'T', 'J', 'Q', 'K', 'A']
    deck = []
    for suit in suits:
        for denomination in denominations:
            ds = denomination + suit
            deck.append(ds)

    random.shuffle(deck)      
    return deck

def deal_cards(deck, players):
    hands = []

    for j in range(players):
        player_hand = []
        for i in range(2):
            cards = deck.pop(0)
            player_hand.append(cards)

        hands.append(player_hand)  
    return hands 
j08691
  • 204,283
  • 31
  • 260
  • 272
user9042207
  • 101
  • 3
  • 10

1 Answers1

3

You need to swap your for loops so you iterate over all players for the first cars, then the second:

def deal_cards(deck, players):
    hands = [[] for player in range(players)]

    for _ in range(2):
        for hand in hands:
            hand.append(deck.pop(0))

    return hands 
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • 1
    Problem here is the implementation of `player_hand`. You start the iteration, instantiate `player_hand`, then for each player you append that hand. You need a new hand for each player. A `Player` class with a `hand` attribute and `add_to_hand()` method would be clean. – pstatix Dec 05 '17 at 04:05
  • Yes, I fixed it now. – Reblochon Masque Dec 05 '17 at 04:06
  • 1
    Yea for all intensive purposes that works. But how do we know which player has what hand? I leave that to the OP to work out in class design. Solution answers the question. – pstatix Dec 05 '17 at 04:07
  • The hands are in the same order as the players, assuming players are an ordered collection – Reblochon Masque Dec 05 '17 at 04:09
  • Aren't the hands and players just parallel lists here? player `0` would correlate to the first position in `hands`? – RoadRunner Dec 05 '17 at 04:10
  • Yes they are, you can assign each hand to each player in sequence. – Reblochon Masque Dec 05 '17 at 04:11
  • @RoadRunner Yes. The point I am trying to make is we take in the players parameter, and use it as what? A count? Why not `numPlayers` vice `players`? My 0.02 on a class design for a case like this is all. Solution does solve the OP's question. – pstatix Dec 05 '17 at 04:13
  • 1
    I agree with you @pstatix, OO is well suited for this application; however, we will need to wait for the OP's questions when he decides to change his design, faces difficulties, and requests help on SO. – Reblochon Masque Dec 05 '17 at 04:15
  • @pstatix Cool., I was just making sure. – RoadRunner Dec 05 '17 at 04:29
  • I think this answer needs to use `hands = [[] for _ in range(players)]` if `players` is a number, rather than a list of player names or something (as the original code suggests). – Blckknght Dec 05 '17 at 06:57