我试图检查用户输入的输入是单个字母或单词,而不是整数或什么都没有,如果他们输入的内容无效,那么他们应该保持在循环中,直到他们输入一个字母。到目前为止,这是我的代码,但它似乎没有以预期的方式工作:
animalcount = 0
animal = 0
data = False
while data == False:
try:
letter = str(input("what letter would you like to search for? "))
data = True
except:
print ("please enter a letter, try again.")
if letter == "":
print ("please enter an item,try again!")
data = False
for animal in animallist:
if letter in animal:
print(animal)
animalcount = animalcount + 1
if animalcount == 0:
print ("That letter cannot be found")