Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 循环用户输入以获得多个输入,而无需多次运行_Python 3.x_Loops_Input - Fatal编程技术网

Python 3.x 循环用户输入以获得多个输入,而无需多次运行

Python 3.x 循环用户输入以获得多个输入,而无需多次运行,python-3.x,loops,input,Python 3.x,Loops,Input,在Python3.8中有没有一种将输入放入循环的方法 我想获得多次用户输入,而不必多次运行程序 我想输入一个英雄,并得到一个答案回来,然后能够进入另一个没有击中运行一遍又一遍 如果有人有任何建议,请告诉我 谢谢大家! """ This program is intended to compare heroes from Marvel and DC. Enter your hero choice and see what hero from #Marvel or DC is most lik

在Python3.8中有没有一种将输入放入循环的方法

我想获得多次用户输入,而不必多次运行程序

我想输入一个英雄,并得到一个答案回来,然后能够进入另一个没有击中运行一遍又一遍

如果有人有任何建议,请告诉我

谢谢大家!

"""

This program is intended to compare heroes from Marvel and DC. Enter 
your 
hero choice and see what hero from #Marvel or DC is most like them.
I know there are simpler ways to write this code so if anyone has any 
suggestions please leave them for me
Thanks for viewing!

Only the heroes below can be entered

marvel = ['Moon Knight', 'Hyperion', 'Thor', 'Punisher', 'Jean Grey', 
'Iron Man', 'Quicksilver']

DC = ['Batman', 'Superman', 'Shazam', 'Red Hood', 'Wonder Woman', 
'Batwing', 'Flash']
"""

choice = str(input('Choose a hero\n'))


def hero_choose():
    if choice.lower() == 'batman':
        return('Moon Knight')
    if choice.lower() == 'moon knight':
        return('Batman')
    if choice.lower() == 'superman':
        return('Hyperion')
    if choice.lower() =='hyperion':
        return('Superman')
    if choice.lower() == 'thor':
        return('Shazam')
    if choice.lower() == 'shazam':
        return('Thor')
    if choice.lower() == 'red hood':
        return('punisher')
    if choice.lower() == 'punisher':
        return('Red Hood')
    if choice.lower() == 'wonder woman':
        return('Jean Grey')
    if choice.lower() == 'jean grey':
        return('Wonder Woman')
    if choice.lower() == 'iron man':
        return('Batwing')
    if choice.lower() == 'batwing':
        return('Iron Man')
    if choice.lower() == 'flash':
        return('Quicksilver')
    if choice.lower() == 'quicksilver':
        return('Flash')
    else:
        return('Your hero may not be available\nor your spelling may be 
wrong.')

print(hero_choose())

您是否考虑过让用户输入逗号分隔的值列表?比如说

$ python choose_hero.py
Please enter hero names seperated by a "," (e.g. batman,robin)
Names: batman,superman
Moon Night
Hyperion
执行情况如下:

print("Please enter hero names seperated by a \",\" (e.g. batman,robin)")
choices = [name for name in str(input('Names: ')).split(',')]
for choice in choices:
  # your implementation of hero_choose will need to be modified 
  # to accept a param, rather than using the global choice var
  hero_choose(choice)

我知道这不是代码审查堆栈,但如果我能提出与您的问题无关的建议。考虑使用一个代替无穷无尽的if语句< /p>是的,我考虑使用字典,但是对于Python来说是很新的,我不太确定如何使用字典。我会试试你的第一个建议,看看效果如何。谢谢。@CodyGreathouse附带的链接应该会给你一些使用词典的指导。希望这解决了你的问题!如果是,请务必将问题标记为已回答