如何使用输入答案作为变量来打印变量列表中的项目

问题描述 投票:0回答:1

这是一个电影选择器程序,我几乎完成了,但是代码很糟糕,所以我决定重新开始并使步骤更简单,所以我决定只使用一些函数变量来实现,而不是为每个超级英雄创建 if 函数从输入答案。 所以我想使用 Specific_qst 的答案来运行 Superhero 类中的超级英雄标题。 例如,如果用户在特定_qst中输入:spiderman,我想从spiderman_title列表中打印一个选择,但我不知道该怎么做。 如果用户随机输入,我只想从所有超级英雄标题列表中获得随机选择

from random import choice

class Superhero:
    spiderman_title=['Spiderman 1', 'Spiderman 2', 'Spiderman 3']
    batman_titles = ['Batman (1966)', ' Batman(1989)', 'The Batman', 'Batman & Robin','Batman Returns','Batman Forever', 'Batman Begins', 'Batman vs. Superman: Dawn of Justice']


question = input('What genre do you want to watch? ')
specific_qst=input('Do you want random pick or a specific superhero, if random, just type random, if specific superhero type his name: ')
def superhero_title():
    title=specific_qst
    if title == Superhero:
        print(choice(title))  #<---- here is the problem
    
   
if question == 'superhero':
    superhero_title()
elif question == 'Superhero":
    superhero_title()

如果有人帮助我,我会很高兴。谢谢大家

我在没有列表的情况下尝试了它,但无法得到正确的答案,我在互联网上搜索了几个小时如何解决这个问题,但没有找到任何有用的东西。如果这是某种愚蠢的问题,我很抱歉,但我很迷茫..

python class variables random input
1个回答
0
投票
  1. 当您将 title 变量与 Superhero 进行比较时,您是将其与类本身进行比较,而不是与它的实例进行比较。相反,您应该将其与特定的超级英雄名称进行比较。

  2. 您需要根据用户选择的超级英雄访问正确的标题列表。

© www.soinside.com 2019 - 2024. All rights reserved.