字典绝对是简化这段代码的最好方法。可以使用输入作为键设置所有选项,并使用默认参数
dict.get
要在出错时返回消息,请执行以下操作:
choice = str(input('Choose a hero\n'))
hero_choose = { 'batman' : 'Moon Knight',
'moon knight' : 'Batman',
'superman' : 'Hyperion',
'hyperion' : 'Superman'
# ...
}
hero = hero_choose.get(choice.lower(), 'Your hero may not be available\nor your spelling may be wrong.')
print(hero)