gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:13 28 4
gpt4 key购买 nike

我正在浏览一些关于 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

关于python - 我如何搜索包含单词 'egg' 或 'eggs' 的所有内容的食谱并打印包含变量的食谱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509219/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com