Python重复动作
我有一个包含字符串的列表Python重复动作,python,python-3.x,while-loop,repeat,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 quest
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()
只有当用户输入按列表顺序排列时,这种代码才起作用。我希望用户能够随机输入。您希望使用“while True”。例如:
while True:
name = input("Enter your name: ")
print("Hi, {}!".format(name))
print("What about now?")
我真不敢相信会那么容易。谢谢!