我希望循環打破,如果任何來自輸入的話都沒有在文本文件中找到讀書,所以到目前爲止,我有:如何打破while循環從文本文件
import re
TypeFood = input("What food would you like to order?")
words = TypeFood.split()
with open("HungryHorseMenu.txt", "r") as f:
for line in f:
if any(word.lower() in re.findall('\w+', line.lower()) for word in words):
splitted_line = line.split(',')
print(splitted_line[0])
else:
print("We do not serve what you would like to eat.")
break
我的文字文件是:
Vegetarian pizza,
Margarita pizza,
Meat feast pizza,
Spaghetti bolognese, pasta, italian
Spaghetti carbonara, pasta, italian
Tagliatteli, pasta, italian
Cheeseburger, american
Chicken burger, american
Veggie burger, american
Hot dog, american
Chicken curry, indian
Lamb curry, indian
Vegetable curry, indian
當我輸入的東西是不是在文本文件時,它不會打破,但如果我輸入的東西是文本文件,它打印出菜單,而且在最後打印我們不提供你想要什麼。 例如
I would like pizza
輸出:
Vegetarian pizza
Margarita pizza
Meat feast pizza
We do not serve what you would like to eat
這工作的感謝,但請你加我一些意見,解釋你添加的行做的,因爲我是個初學者,我不明白他們在做什麼。 – Thy