一副扑克牌,将大小王抽出。把剩余的52张牌随机发给4名玩家,并显示每名玩家收到牌的情况。python代码
时间: 2024-10-08 16:10:32 浏览: 43
在Python中,我们可以先排除大王和小王,然后将剩余的52张牌平均分配给4位玩家。这里是一个例子代码:
```python
import random
# 定义扑克牌没有大小王的列表
suits = ['♠', '♥', '♦', '♣']
ranks = list(range(2, 10)) + list(range('J', 'A+1'))
# 初始化一副扑克牌,去除大小王
deck = [f'{rank}{suit}' for suit in suits for rank in ranks]
deck.remove('J♠') # 排除小王
deck.remove('Q♠') # 排除大王
# 确保有足够牌供4人分发
if len(deck) < 4 * 13:
print("Not enough cards after removing the jokers.")
else:
# 将剩余的牌分成4份
players = [[] for _ in range(4)]
# 随机分配每名玩家的牌
while deck:
player_index = random.randint(0, 3)
players[player_index].append(deck.pop(random.randint(0, len(deck)-1)))
# 显示每位玩家的牌
for i, player in enumerate(players):
print(f"Player {i+1}: {player}")
#
阅读全文