Python新手。在一个while循环中,我要求用户输入一个字典的关键字。然后打印该键的值。这个过程应该继续下去,直到输入与字典中的任何键不匹配。我使用if语句来查看密钥是否在字典中。如果不是,我不喜欢while循环打破。到目前为止,我无法让它突破。Python用户输入打破while循环
谢谢大家
Animal_list = {
'lion': 'carnivora', 'bat': 'mammal', 'anaconda': 'reptile',
'salmon': 'fish', 'whale': 'cetaceans', 'spider': 'arachnida',
'grasshopper': 'insect', 'aligator': 'reptile', 'rat': 'rodents',
'bear': 'mammal', 'frog': 'amphibian', 'turtles': 'testudines'
}
while True:
choice = raw_input("> ")
if choice == choice:
print "%s is a %s" % (choice, Animal_list[choice])
elif choice != choice:
break
作为@christopher曾建议我也会建议一样,Python有“中”经营者,这是用来按顺序检查会员,字符串,元组etcetra。你可以在这个链接检查例子:https://www.tutorialspoint.com/python/membership_operators_example.htm –