Python重复操作

时间:2017-04-27 23:10:39

标签: python python-3.x while-loop repeat

我有一个包含字符串的列表

animalList=['ASLAN', 'KAPLAN', 'KOPEK', 'KEDI']
descLion = ( 'it is called lion....')
descTiger = (' it is called tiger....')

我要求用户输入其中一个并检查拼写错误

questOne = input("Enter the name of the animal: ")
questOne = questOne.upper()
while questOne not in animalList: 
    questOne = input("Whatch out for typos Try again: ")
    questOne = questOne.upper()
else:
    print(questOne + ' IT IS')

我无法弄清楚的是,我希望我的代码不断询问动物名称,检查拼写错误并打印相关说明并重复此操作。我尝试过类似的东西;

while questOne == animalList[0]:
    print (descLion)
    questOne = input("Enter the name of the animal: ")
    questOne = questOne .upper()

while questOne == animalList[1]:
    print (descTiger)
    questOne = input("Enter the name of the animal: ")
    questOne = questOne.upper()

此代码类型仅在用户输入符合列表顺序时才有效。我希望用户能够随机输入输入。

1 个答案:

答案 0 :(得分:1)

你想使用"而True"。例如:

while True:
    name = input("Enter your name: ")
    print("Hi, {}!".format(name))
    print("What about now?")