首页 > 解决方案 > 从程序列表中删除“随机”和“输入”值

问题描述

我最近和一些朋友一起玩了狼人游戏,为了简化游戏大师的工作,我想创建一个简单的 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个问题:

标签: pythonlistrandominputcounter

解决方案


我不是 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))

推荐阅读