-1

Is there a way to put input into a loop in python 3.8?

I want get to user input multiple times without having to run the program more than once.

I would like to enter a hero and get an answer back and then be able to enter another one without hitting run over and over.

If anyone has any suggestions please let me know.

Thank you!

"""

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

1 Answers1

0

Have you considered having your user enter a comma delimited list of values? For instance

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

implemented as follows

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)

I know this isn't the Code Review stack, but if I could make a suggestions unrelated to your issue. Consider using a Dictionary as a switch statement instead of an endless stream of if statements

Bitsplease
  • 306
  • 2
  • 12
  • Yes I considered using a dictionary but being pretty new to python I'm not quite sure how to use one. And I'll try your first suggestion and see how it works. Thank you. – Cody Greathouse Feb 04 '20 at 01:46
  • @CodyGreathouse the attached link should give you a bit of guidance on using the dictionary. Hopefully that solved your problem! If so, be sure to mark the question as answered – Bitsplease Feb 04 '20 at 16:48