Python 如何中断从文本文件读取的while循环

Python 如何中断从文本文件读取的while循环,python,python-3.x,Python,Python 3.x,如果在文本文件中找不到输入中的任何单词,我希望循环中断,到目前为止,我有: 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

如果在文本文件中找不到输入中的任何单词,我希望循环中断,到目前为止,我有:

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
当我键入不在文本文件中的内容时,它会断开,但如果我键入文本文件中的内容,它会打印出菜单,但最后也会打印出我们不提供您想要的服务。 e、 g

输出:

Vegetarian pizza 
Margarita pizza   
Meat feast pizza
We do not serve what you would like to eat

你在每一行中循环,当它到达不包含你的短语的第一行时,即使它发现了什么,它也会给出一个错误。你想要这样的东西:

import re
TypeFood = input("What food would you like to order?")
words = TypeFood.split()
with open("HungryHorseMenu.txt", "r") as f:
    found = False
    for line in f:
        if any(word.lower() in re.findall('\w+', line.lower()) for word in words):
            splitted_line = line.split(',')
            found = True
            print(splitted_line[0])
    if found==False:
        print("We do not serve what you would like to eat.")

尝试反转验证:

for line in f:
    if !any(word.lower() in re.findall('\w+', line.lower()) for word in words):
        print("We do not serve what you would like to eat.")
        break
    else:
        splitted_line = line.split(',')
        print(splitted_line[0])

您可以将所有有效项放入列表中。如果列表为空,表示没有有效项,如果不为空,则打印所有有效项

import re
TypeFood = input("What food would you like to order?")
words = TypeFood.split()
with open("sample.csv", "r") as f:
    valid_items = list()
    for line in f:
        if any(word.lower() in re.findall('\w+', line.lower()) for word in words):
            splitted_line = line.split(',')
            valid_items.append(splitted_line[0])


    if not valid_items:
        print("We do not serve what you would like to eat")
    else:
        for item in valid_items:
            print(item)
产出:

python test.py                                           
What food would you like to order?pizza
Vegetarian pizza
Margarita pizza
Meat feast pizza

python test.py                                 
What food would you like to order?indian
Chicken curry
Lamb curry
Vegetable curry

python test.py                                  
What food would you like to order?canadian
We do not serve what you would like to eat

使用
而不是
逻辑中断。尝试输入
pizza
,它会给出你的
我们不…
,这是不正确的。这很有效,谢谢,但是你能给我添加一些注释来解释你添加的行是做什么的吗,因为我是一个初学者,我不明白它们是做什么的。
python test.py                                           
What food would you like to order?pizza
Vegetarian pizza
Margarita pizza
Meat feast pizza

python test.py                                 
What food would you like to order?indian
Chicken curry
Lamb curry
Vegetable curry

python test.py                                  
What food would you like to order?canadian
We do not serve what you would like to eat