我正在浏览一些关于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
非常感谢帮助,因为我有一个需要食谱的女朋友,我说id比她能买的更好lol
提前谢谢
答案 0 :(得分:1)
您可以使用正则表达式模块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