|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
动动手的第1题 发牌程序
https://fishc.com.cn/thread-207298-1-2.html
不理解的那行代码在后面已注释
- import random
-
- cards = ["♦1", "♦2", "♦3", "♦4", "♦5", "♦6", "♦7", "♦8", "♦9", "♦10", "♦J", "♦Q", "♦K",
- "♥1", "♥2", "♥3", "♥4", "♥5", "♥6", "♥7", "♥8", "♥9", "♥10", "♥J", "♥Q", "♥K",
- "♣1", "♣2", "♣3", "♣4", "♣5", "♣6", "♣7", "♣8", "♣9", "♣10", "♣J", "♣Q", "♣K",
- "♠1", "♠2", "♠3", "♠4", "♠5", "♠6", "♠7", "♠8", "♠9", "♠10", "♠J", "♠Q", "♠K",
- "☀", "🌙"]
-
- def fy_shuffle(x, n=1):
- for i in range(n):
- target = list(x)
- result = []
- while target:
- r = random.randint(0, len(target)-1) # 步骤2
- result.append(target.pop(r)) # 步骤3
-
- return result
-
- def dealCards():
- a = input("请输入第一位游戏玩家名称:")
- b = input("请输入第二位游戏玩家名称:")
- c = input("请输入第三位游戏玩家名称:")
-
- r = {}
- r[a], r[b], r[c] = [], [], []
-
- new_cards = fy_shuffle(cards, 3)
-
- for i in range(17):
- r[a].append(new_cards.pop())
- r[b].append(new_cards.pop())
- r[c].append(new_cards.pop())
-
- d = random.sample((a, b, c), 1)[0] 这个[0]不理解,为啥要在后面加一个下标索引[0]
- print(f"\n地主是:{d}\n")
- r[d].extend((new_cards.pop(), new_cards.pop(), new_cards.pop()))
-
- print(f"[{a}]拿到的牌是:{' '.join(r[a])}\n")
- print(f"[{b}]拿到的牌是:{' '.join(r[b])}\n")
- print(f"[{c}]拿到的牌是:{' '.join(r[c])}")
-
- dealCards()
复制代码
根据你提供的代码,我发现标识为 的变量是一个包含三个元素的列表( - random.sample((a, b, c), 1)
复制代码),而 表示取该列表中的第一个元素。即,这行代码是用来获取在 三个游戏玩家名称中随机选择出来的地主名称。
实际上,通过 code]random.sample((a, b, c), 1)[/code] 可以从 [code[/code] 这个元组中无重复地随机选取一个元素形成一个列表,并且 表示取这个列表中的第一个元素。因此,代码中的 用于获得一个随机选取出来的地主名称。
希望能帮助到你!如果还有其他问题,请随时提问。
|
|