我在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
答案 0 :(得分:3)
你需要交换你的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