Skip to main content
 首页 » 编程设计

python之我如何搜索包含单词 'egg' 或 'eggs' 的所有内容的食谱并打印包含变量的食谱

2025年05月04日11langtianya

我正在浏览一些关于 python 的书籍,以便自动执行任务,例如在书中查找短语和对不同的事物使用不同的正则表达式。我觉得编程在任何领域都有用。然而,现在我正在整理一本食谱,我想知道一些事情。第一个我可能会弄清楚自己“我如何编写我的打印功能,这样它就不会是一条没完没了的线?”而且,我的主要问题是“我将如何格式化此代码以在输入时打印出所有带有“鸡蛋”或“鸡蛋”一词的食谱?”

while True: 
    response = input() 
    if response == 'scrambled eggs': 
        print('2 eggs, 1 teaspoon mayonnaise or salad dressing, 1 teaspoon water (optional), 1 teaspoon butter, salt and pepper to taste. In a cup or small bowl, whisk together the eggs, mayonnaise and water using a fork. Melt margarine in a skillet over low heat. Pour in the eggs, and stir constantly as they cook. Remove the eggs to a plate when they are set, but still moist. Do not over cook. Never add salt or pepper until eggs are on plate, but these are also good without ') 
    if response == 'rice patties': 
        print('1 cup cooked rice, chopped 1/4 cup shredded Cheddar cheese, 1 egg beaten, 1 teaspoon minced garlic, 1/4 teaspoon salt, 1/4 teaspoon ground black pepper, 1/4 teaspoon chopped fresh parsley, 1 tablespoon vegetable oil. Step one: Mix rice, onion, Cheddar cheese, egg, garlic, salt, black pepper, and parsley in a bowl. Step two: Cover bowl with plastic wrap and refrigerate at least 30 minutes. Step three: Form rice mixture into 4 small patties. Step four: Heat vegetable oil in a large skillet over medium-high heat. Fry patties in hot oil until lightly browned, about 5 minutes per side.') 
    if response == 'french toast': 
        print('bread, eggs, milk, vanilla, syrup, butter, cinnamon') 
    if response == 'scrambled eggs': 
        print('eggs, cheese') 
    if response == 'close': 
        break 

非常感谢您的帮助,因为我有一个女朋友需要一本食谱,我说我做的比她买的更好,哈哈

提前致谢

请您参考如下方法:

您可以使用正则表达式模块更改您的代码 re :

import re 
 
rx = r'\beggs?\b' 
# looks for egg or eggs as a word 
 
while True: 
    response = input() 
    if re.search(rx, response): 
        print "Yummy eggs in here!" 
    if response == 'scrambled eggs': 
        print('2 eggs, 1 teaspoon mayonnaise or salad dressing, 1 teaspoon water (optional), 1 teaspoon butter, salt and pepper to taste. In a cup or small bowl, whisk together the eggs, mayonnaise and water using a fork. Melt margarine in a skillet over low heat. Pour in the eggs, and stir constantly as they cook. Remove the eggs to a plate when they are set, but still moist. Do not over cook. Never add salt or pepper until eggs are on plate, but these are also good without ') 
    if response == 'rice patties': 
        print('1 cup cooked rice, chopped 1/4 cup shredded Cheddar cheese, 1 egg beaten, 1 teaspoon minced garlic, 1/4 teaspoon salt, 1/4 teaspoon ground black pepper, 1/4 teaspoon chopped fresh parsley, 1 tablespoon vegetable oil. Step one: Mix rice, onion, Cheddar cheese, egg, garlic, salt, black pepper, and parsley in a bowl. Step two: Cover bowl with plastic wrap and refrigerate at least 30 minutes. Step three: Form rice mixture into 4 small patties. Step four: Heat vegetable oil in a large skillet over medium-high heat. Fry patties in hot oil until lightly browned, about 5 minutes per side.') 
    if response == 'french toast': 
        print('bread, eggs, milk, vanilla, syrup, butter, cinnamon') 
    if response == 'scrambled eggs': 
        print('eggs, cheese') 
    if response == 'close': 
        break