Python 重复操作
Python Repeated Actions
我有一个包含字符串的列表
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?")
我有一个包含字符串的列表
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?")