使用python,定义函数随机生成一副扑克牌(去掉大小王),并分给4位牌手,输出。
时间: 2024-05-12 14:13:15 浏览: 224
PHP实现随机发扑克牌
下面是一个示例代码,使用 Python 的 random 模块来实现随机生成和洗牌。其中,我们定义了一个 poker_generator 函数来生成一副不包括大小王的扑克牌,并使用 random.sample 函数来从牌中随机抽取 13 张牌给每位牌手:
```python
import random
# 定义扑克牌
suits = ['♠', '♥', '♣', '♦']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
def poker_generator():
"""生成一副扑克牌"""
return [(suit, rank) for suit in suits for rank in ranks]
def deal():
"""洗牌并分给4位牌手"""
poker = poker_generator()
random.shuffle(poker)
hands = [poker[i:i+13] for i in range(0, 52, 13)]
for i, hand in enumerate(hands):
print(f'Player {i+1}: {hand}')
deal()
```
运行结果如下:
```
Player 1: [('♥', '3'), ('♦', 'Q'), ('♠', '6'), ('♥', '7'), ('♣', '8'), ('♣', '6'), ('♠', '3'), ('♥', 'J'), ('♦', '8'), ('♦', 'J'), ('♠', 'J'), ('♣', 'J'), ('♦', 'A')]
Player 2: [('♦', '2'), ('♠', '2'), ('♣', '7'), ('♥', '9'), ('♠', 'K'), ('♣', '3'), ('♥', '10'), ('♠', 'A'), ('♦', 'K'), ('♣', '10'), ('♥', '6'), ('♠', '9'), ('♦', '5')]
Player 3: [('♣', '4'), ('♦', '7'), ('♥', 'K'), ('♠', '7'), ('♦', '6'), ('♠', '8'), ('♦', '10'), ('♣', '9'), ('♥', '8'), ('♣', 'A'), ('♥', 'A'), ('♠', 'Q'), ('♣', '5')]
Player 4: [('♥', '2'), ('♠', '5'), ('♣', 'K'), ('♦', '9'), ('♥', 'Q'), ('♠', '4'), ('♦', '4'), ('♥', '5'), ('♣', 'Q'), ('♠', '10'), ('♦', '3'), ('♥', '4'), ('♣', '2')]
```
阅读全文