我的搜索栏会打印每个变量,而不仅仅是您键入的变量

时间:2019-09-01 07:56:32

标签: python-3.x

我的搜索栏无法工作

我正在制作超级英雄百科全书,目前正在搜索栏上工作。

for x in range(1, 2):
    choose_hero = input("Select your hero:  ")
    superhero_batman = print("real name: Bruce Wayne has no powers is in DC universe")
    superhero_antman = print("real name:Scott lang powers: suit enables him to change size and communicate with some insects like ants universe is Marvel")
    superhero_hulk = print("real name: Bruce Banner powers super strength can leap miles can sonic clap universe is Marvel")
    heroes = ['superhero_batman', 'superhero_antman', 'superhero_hulk']
    hero = input("Select your item: ")
if choice in heroes:
    print(hero)
else:
    print("unidentified")

当您在列表中键入英雄时,它将在其中打印每个超级英雄,而不是仅打印您键入的英雄。我如何只打印您键入的内容?

1 个答案:

答案 0 :(得分:0)

对于要实现的目标,建议您使用字典

hero_info = {
    "batman": "real name: Bruce Wayne has no powers is in DC universe",
    "antman": "real name:Scott lang powers: suit enables him to change size and communicate with some insects like ants universe is Marvel"
    ...
}

现在您可以在方括号中使用想要的英雄的名字访问字典中的信息,如下所示:

print(hero_info["batman"])
>>> real name: Bruce Wayne has no powers is in DC universe

完整的程序可能如下所示:

hero_info = {
    "batman": "real name: Bruce Wayne has no powers is in DC universe",
    "antman": "real name:Scott lang powers: suit enables him to change size and communicate with some insects like ants universe is Marvel"
    ...
}

choose_hero = input("Select your hero: ")

if choose_hero in hero_info:
    print(hero_info[choose_hero])
else:
    print("This hero is not in my database.")