我用 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
我是一名优秀的程序员,十分优秀!