这个搜索功能有效,但是如果我搜索一个叫Sarah的动物,名单中有两只动物名为Sarah,它只打印其中一只。我该怎么做才能打印所有搜索匹配?
def search():
choice = int(input("""
1 Name
2 Age
3 Specie
4 Gender
What do yo want to search for? """))
if choice == 1:
your_search = input("Write the animal name: ").capitalize()
for Animals in animal_list:
if your_search == Animals.name:
print(Animals)
break
else:
print("Sorry, couldn't find any animal called " + your_search + ", maybe check the spelling.")
答案 0 :(得分:0)
尝试在每次成功比赛后删除休息
答案 1 :(得分:0)
您告诉程序在找到结果后停止搜索。只需删除break命令并添加一个标记,说明是否找到了名称:
def search():
choice = int(input("""
1 Name
2 Age
3 Specie
4 Gender
What do yo want to search for? """))
if choice == 1:
your_search = input("Write the animal name: ").capitalize()
found = False
for Animals in animal_list:
if your_search == Animals.name:
found = True
print(Animals)
if found == False:
print("Sorry, couldn't find any animal called " +
your_search + ", maybe check the spelling.")