Python 编程练习与字典知识详解
一、卡牌相关编程练习
- 创建完整的卡牌列表
- 编写一个名为
createDeck的函数,使用循环将 52 张卡牌的双字符缩写存储到一个列表中,最后返回该列表。此函数无需参数。
python def createDeck(): suits = ['s', 'c', 'd', 'h'] ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] deck = [] for suit in suits: for rank in ranks: deck.append(rank + suit) return deck
- 编写一个名为
-
洗牌函数
- 编写一个名为
shuffle的函数,用于随机打乱卡牌列表的顺序。不能使用 Python 内置的shuffle函数,需自己编写循环实现。以下是一种实现方式:
```python
import random
def shuffle(deck):
for i in range(len(deck)):
j = random.randint(i, len(deck) - 1)
deck[i], deck[j] = deck[j], deck[i] - 编写一个名为
超级会员免费看
订阅专栏 解锁全文
7

被折叠的 条评论
为什么被折叠?



