Python初学者输入和不同的选择

时间:2016-05-20 11:31:04

标签: python

虽然可能有一种更简单的方法,但我希望了解有什么问题。代码应该告诉你超级英雄的真实身份。

问题在于:

在您提供超级英雄的真实姓名后,它会询问“您是否需要更多信息?”;你如何设置这个问题的选择?

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')

5 个答案:

答案 0 :(得分:1)

将嵌套条件与另一个变量一起使用,例如:选择2

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

答案 1 :(得分:0)

问题是你使用" elif"而不是嵌套"如果"检查,如果第二个选择是"是"。

如果将代码拆分为较小的块(函数),将会很有帮助。然后你可以写这样的代码:

{{1}}

答案 2 :(得分:0)

我会这样试试:

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')

答案 3 :(得分:0)

如果您的任何elif条件满足,则不会检查下一个elif条件 如果您输入: Nome Supereroe:Bruce Banner 它满足elif选择==' Bruce Banner':条件,你的输入是 Desideri maggiori informazioni?是的 我不会检查elif选择=='是':条件,因为它已满足您之前的条件。

答案 4 :(得分:0)

如果再次使用输入功能并分配给不同的变量,则不会影响原始输入。你可以在if块结束的地方,即低于其他地方,询问这个问题。例如..

MDA