Python初学者输入和不同选择

Python初学者输入和不同选择,python,Python,虽然可能有一种更简单的方法,但我想知道错在哪里。当给出超级英雄的名字时,代码应该告诉你超级英雄的真实身份 问题是: 在你提供超级英雄的真实姓名后,它会问“你需要更多信息吗?”;您如何设置此问题的选项 super_heros = {'Hulk': 'Bruce Banner', 'Capitan America': 'Steve Rogers', 'Spiderman': 'Peter Parker'} hero_biography =

虽然可能有一种更简单的方法,但我想知道错在哪里。当给出超级英雄的名字时,代码应该告诉你超级英雄的真实身份

问题是:

在你提供超级英雄的真实姓名后,它会问“你需要更多信息吗?”;您如何设置此问题的选项

super_heros = {'Hulk': 'Bruce Banner',
               'Capitan America': 'Steve Rogers',
               'Spiderman': 'Peter Parker'}
hero_biography = {'Bruce Banner' : 'David Banner nasce in California...ecc'}

while True:
    choice = input('Nome Supereroe:') ###Superhero's name:
    if choice == 'Hulk':
        print(super_heros['Hulk'])
    elif choice == 'Bruce Banner':
        choice = input('Desideri maggiori informazioni?') ###Do you want more information
    elif choice == 'Yes': ### I know that this one will refer to : choice = input('Nome Supereroe:')
        print(hero_biography['Bruce Banner'])
    elif choice == 'Capitan America':
        print(super_heros['Capitan America'])
    elif choice == 'Spiderman':
        print(super_heros['Spiderman'])
    elif choice == 'Esc':
        break
    else:
        choice == ''
        print('Nome inesistente')

将嵌套条件与其他变量一起使用,例如choice2

...
elif choice == 'Bruce Banner':
    choice2 = input('Desideri maggiori informazioni?')
    if choice2 == "Yes":
        print(hero_biography['Bruce Banner'])
elif choice == 'Captain America':
...

问题是,如果第二个选择是“是”,则使用“elif”而不是嵌套的“if”进行检查

如果您将代码分成更小的块(函数),这将非常有用。然后您可以这样编写代码:

choice = input('Nome Supereroe:')
while choice != 'Esc':
    printCharacterInfo(choice)
    choice = input('Nome Supereroe:')

def printCharacterInfo(character):
    try:
        print(super_heros[character])
    except KeyError:
        if character in hero_biography:
            proposeBiographyInformation(character)

        else:
            print('Nome inesistente')

def proposeBiographyInformation(name):
    if input('Desideri maggiori informazioni?') == 'Yes':
        print(hero_biography[name])

我会这样尝试:

while True:
    choice = input('Nome Supereroe:')

    if choise in super_heros:
        print(super_heros[choise])

    elif choise in hero_biography:
        moreInformation = input('Desideri maggiori informazioni?')
        if moreInformation == 'yes':
            print(hero_biography[choise])

    else:
        print('Nome inesistente')

如果满足任何elif条件,则不会检查下一个elif条件。
如果您的输入: Nome Supereroe:布鲁斯·班纳 它满足elif choice=='Bruce Banner':条件,您在这里的输入是 Desideri maggiori informazioni?是的

我将不检查elif choice=='Yes':条件,因为它已经满足您以前的条件。

如果您再次使用输入函数并分配给不同的变量,它将不会影响原始输入。您可以在if块结束的地方,即else下方,询问此问题。例如

...
else:
        choice == ''
        print('Nome inesistente')

newChoice = input('Do you need more information?')
#your code to handle new choice user entered.

您需要了解的是,一旦
if else
块找到对应关系,即验证块中的第一个条件,它将跳过剩余的
elif
,循环将进入下一次迭代。您不需要if语句。只需执行
print(超级英雄[choice])
我会在try语句中使用它,以便在密钥不存在时捕获异常。只有当用户输入错误的英雄名称时,您才会要求显示更多信息。这看起来是错误的。问题是:我想插入一行,这样可以获得关于超级英雄的更多信息,如果你在其中输入真实姓名-我假设真实姓名和超级英雄姓名集是一个空集。你是对的,我误解了这个问题。你使用try-catch块的方式看起来仍然。。。有趣:)我已经尝试过了,但是当我运行它时,我得到了这样一条消息:名称“choice2”无法定义。