python 如何让用户在程序中的嵌套列表之间进行选择?

bn31dyow  于 2023-03-07  发布在  Python
关注(0)|答案(2)|浏览(80)

我试图创建一个游戏,用户可以先在3个不同的Map之间进行选择,然后输入像"left"和"grab"这样的单词来移动所选的Map(就像一个基于文本的游戏)。大多数程序使用列表方法和if语句。我被卡住的部分是Map选择,它使用嵌套列表。
我是这么试的:

choose = input("What floor plan would you like?: 1, 2, or 3?: ")
if choose == 2:
    2 == plan2
    plan = plan.replace(plan, plan2)
elif choose == 3:
    plan = plan.replace(plan,plan3)

这是一个嵌套列表的例子

plan = [
    ['nothing', 'a wooden stake', 'a wooden stake','a vampire','stairs leading up'],
    ['stairs leading down', 'a vampire','a wooden stake', 'a vampire','stairs leading up'],
    ['stairs leading down', 'a silver sword','chocolate','a werewolf','The Wand']
    ]

我尝试了很多方法,但似乎都不起作用。"plan"的使用方式是否会妨碍它正常工作?我曾将其用作"plan [floor][room]",它会跟踪玩家的位置。我不想尝试使用任何过于复杂的东西,因为我还在学习中。但是让我知道replace()列表方法是否在这里不起作用。2如果这个问题太模糊或者需要重新措辞或者删除,请让我知道。3谢谢!

lrpiutwd

lrpiutwd1#

创建三个 * 独立 * 变量

每一个都包含一个平面图。请选择您想要的平面图。
不要试图做任何事情,那会给你带来麻烦。

plan1 = [
    ['nothing', 'a wooden stake', 'a wooden stake','a vampire','stairs leading up'],
    ['stairs leading down', 'a vampire','a wooden stake', 'a vampire','stairs leading up'],
    ['stairs leading down', 'a silver sword','chocolate','a werewolf','The Wand']
    ]

plan2 = [
    [..., ..., ...],
    [..., ..., ...],
    [..., ..., ...],
    ]

plan3 = [
    [..., ..., ...],
    [..., ..., ...],
    [..., ..., ...],
    ]

choose = input("What floor plan would you like?: 1, 2, or 3?: ")

if choose == 1:
    plan = plan1
elif choose == 2:
    plan = plan2
elif choose == 3:
    plan = plan3
92dk7w1h

92dk7w1h2#

您只需格式化返回的数字并将其用作plan中的索引

plan = (
    ['nothing', 'a wooden stake', 'a wooden stake','a vampire','stairs leading up'],
    ['stairs leading down', 'a vampire','a wooden stake', 'a vampire','stairs leading up'],
    ['stairs leading down', 'a silver sword','chocolate','a werewolf','The Wand']
)

MSG1 = "What floor plan would you like?: 1, 2, or 3?: "

#keep asking the question until a valid answer is given
#format the answer to a plan index
while not (i:=int(input(MSG1))-1) in range(len(plan)):
    pass

this_plan = plan[i]

相关问题