用Python3.7对以下要求进行编程。 用函数随机生成一副扑克牌(去掉大小王),并分给4位牌手,输出。
时间: 2024-06-08 11:09:45 浏览: 90
基于Python制作一副扑克牌过程详解
5星 · 资源好评率100%
可以使用Python中的random模块来随机生成一副扑克牌。以下是实现的代码:
```python
import random
# 定义扑克牌的花色和点数
suits = ['♥', '♠', '♦', '♣']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
# 定义生成扑克牌的函数
def generate_deck():
deck = [(suit, rank) for suit in suits for rank in ranks]
return deck
# 定义发牌函数,随机分给4位牌手
def deal(deck):
random.shuffle(deck)
player_1 = deck[:13]
player_2 = deck[13:26]
player_3 = deck[26:39]
player_4 = deck[39:]
return player_1, player_2, player_3, player_4
# 执行程序,输出结果
if __name__ == '__main__':
deck = generate_deck()
player_1, player_2, player_3, player_4 = deal(deck)
print("Player 1:", player_1)
print("Player 2:", player_2)
print("Player 3:", player_3)
print("Player 4:", player_4)
```
运行以上代码,将会输出四位牌手的牌组,例如:
```
Player 1: [('♠', '9'), ('♣', 'Q'), ('♣', '5'), ('♠', '2'), ('♠', '3'), ('♥', 'J'), ('♥', '4'), ('♠', 'J'), ('♦', 'K'), ('♥', '9'), ('♥', 'A'), ('♥', 'Q'), ('♠', '6')]
Player 2: [('♠', '10'), ('♣', '8'), ('♠', 'K'), ('♠', 'A'), ('♠', 'Q'), ('♣', '9'), ('♥', '2'), ('♦', 'A'), ('♦', '4'), ('♦', '3'), ('♣', 'K'), ('♦', '5'), ('♣', '10')]
Player 3: [('♦', '7'), ('♦', 'J'), ('♣', '3'), ('♦', '6'), ('♣', 'A'), ('♥', '5'), ('♠', '8'), ('♣', 'J'), ('♦', 'Q'), ('♥', '3'), ('♣', '2'), ('♦', '10'), ('♣', '6')]
Player 4: [('♦', '9'), ('♣', '4'), ('♠', '7'), ('♥', 'K'), ('♦', '2'), ('♠', '5'), ('♣', '7'), ('♥', '7'), ('♠', '4'), ('♦', '8'), ('♣', 'Q'), ('♥', '10'), ('♥', '8')]
```
阅读全文