如何在Python中搜索用户输入关键字

hvvq6cgz  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(111)

如何搜索用户输入并找到特定的关键字,然后在文本文件中搜索此关键字?
这就是我目前所拥有的:

foods = str(input("what takeaway would you like today go sir/madame reply with one word answers"))

with open("pizza.txt", "r") as f:              
    searchlines = f.readlines()

for i, line in enumerate(searchlines):      
    if foods in line:  
            print(line)

字符串

qni6mghb

qni6mghb1#

你的第一句话意味着你想限制用户输入可能找到的食物。

dishes = (
    "Patriot Pizza: yellow cheese, tomato, olives",
    "Tropical treat: guava cheese, pineapple, mango",
)

foods = ('cheese', 'tomato', 'olives', 'guava', 'pineapple', 'mango')
print(foods)

food = ''
while food not in foods:
    food = input('Enter one of the foods listed above: ')

for i, dish in enumerate(dishes):
    if food in dish:
        print(i, dish)

字符串

相关问题