python - 从程序列表中删除“随机”和“输入”值
问题描述
我最近和一些朋友一起玩了狼人游戏,为了简化游戏大师的工作,我想创建一个简单的 Python 程序,允许快速角色随机化。
我不是专业人士,也不是具有高 Python 技能的人,我只了解算法的工作原理,所以我缺乏词汇来塑造我的想法,但我设法创建了一个工作代码:
import random
Roles = ['a werewolf', 'the werewolf seer', 'the fool', 'the seer', 'the witch', 'the hunter', 'the priest', 'cupidon', 'the little girl', 'the bodyguard']
List = []
Counter = 0
Players = int(input("How many player are there ? "))
while Counter < Players:
print("Player", Counter + 1, end=", ")
Name = (input("What is your name ? "))
List.append(Name)
Counter = Counter + 1
for i in range(Players):
print(random.choice(List), ", You are", random.choice(Roles))
问题是,角色和名称是随机的,所以我不能告诉我的程序仅排除列出的值,因此有些名称和一些角色是重复的。
我有3个问题:
- 如何引用列表中的输入值?
- 如何引用列表中的随机值?
- 如何告诉我的程序从列表中删除仅列出的值(随机或输入)以避免重复?
解决方案
我不是 100% 确定这是你想要的,我假设你想要每个玩家不同的角色。此外,如果是这种情况,应该检查 Players 不超过角色的数量。
import random
Roles = ['a werewolf', 'the werewolf seer', 'the fool', 'the seer', 'the witch', 'the hunter', 'the priest', 'cupidon', 'the little girl', 'the bodyguard']
PlayerNames = []
Counter = 0
Players = int(input("How many player are there ? "))
while Counter < Players:
print("Player", Counter + 1, end=", ")
Name = (input("What is your name ? "))
PlayerNames.append(Name)
Counter = Counter + 1
for player in PlayerNames:
role = random.choice(Roles)
Roles.remove(role)
print(player, ", You are", random.choice(Roles))
推荐阅读
- django - 如何将我的用户详细信息的个人资料检索到用户的个人资料页面
- javascript - 使用 Helmet 加载 JavaScript 文件不起作用
- image-processing - 如何使用 skimage 在图像上使用阈值标记和绘制边界
- bootstrap-4 - 如何将文本框向右移动
- sql - 根据另一列中的数据创建编号行 [ORACLE]
- python - 在函数参数体中定义的列表是否充当 python 中的静态变量?
- html - 悬停按钮问题
- reactjs - 反应表单验证错误 - 传播不可迭代实例的无效尝试
- html - 需要格式化 HTML 以制作正确的表格形式
- python - 如何在python中匹配人脸