在发第二张牌之前向每个玩家发一张牌 - Python 中的二十一点游戏

Dealing One Card to Each Player Before Dealing a Second Card - Blackjack Game in Python

我在Python中做了如下代码。其中 deck = deck_of_cards()。我的问题是如何修复第二个功能,以便它必须在发第二张牌之前向每个玩家发第一张牌。现在的方式似乎是先给一个玩家发两张牌,然后再给其他玩家发牌,我不知道如何解决。

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 

您需要交换 for 循环,以便遍历所有玩家以获得第一辆汽车,然后是第二辆:

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