【发布时间】:2023-07-20 07:34:01
【问题描述】:
我有一个元组列表,我正在尝试搜索列表并显示包含搜索词的所有结果。到目前为止,我有这个:
holidays = []
holidays.append((1001,"Tenerife","Spain",2, 7, 80))
holidays.append((1002,"Cancun","Mexico",4, 14, 120))
holidays.append((1003,"Pairs","France",2, 3, 75))
holidays.append((1004,"Salzburg","Austria",3, 10, 90))
holidays.append((1004,"Madrid","Spain",3, 10, 90))enter code here
和:
search = input("Please enter a destination city or country to display all matching results: ")
for item in holidays:
if search in item:
print ()
print ("Here are all the results containing the word", search)
print ()
print ("Package ID:", item[0],":", item[1], "-", item[2],":",item[3], "person(s)",
"for", item[4], "nights costing" , "£" + str(item[5]), "pp/night")
print ()
menu = input("Press any key to return to the main menu:")
但如果我搜索西班牙,它只会显示包含西班牙这个词的第一个结果,而不是全部。我是否认为它应该显示包含搜索词的所有结果,因为它位于 for 循环中?
【问题讨论】:
-
input将等待您按回车键,然后在第一个找到的结果后继续循环...我怀疑您想要去凹痕 -
输入确实显示不止一个,只是你的 for 循环中有一个输入