从程序列表中删除 "randomized" 和 "input" 值
Remove "randomized" and "input" values from a procedural list
我最近和一些朋友玩了狼人杀游戏,为了减轻 Game Master 的工作,我想创建一个简单的 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% 确定这就是您想要的,我假设您希望每个玩家扮演不同的角色。此外,如果是这种情况,应该检查玩家是否不超过角色数量。
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))
我认为这适合你:)
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):
rand_names = random.choice(List)
rand_roles = random.choice(Roles)
List.remove(rand_names)
Roles.remove(rand_roles)
print(rand_names, ", You are", rand_roles)
我最近和一些朋友玩了狼人杀游戏,为了减轻 Game Master 的工作,我想创建一个简单的 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% 确定这就是您想要的,我假设您希望每个玩家扮演不同的角色。此外,如果是这种情况,应该检查玩家是否不超过角色数量。
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))
我认为这适合你:)
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):
rand_names = random.choice(List)
rand_roles = random.choice(Roles)
List.remove(rand_names)
Roles.remove(rand_roles)
print(rand_names, ", You are", rand_roles)