2

i am going through some books on python in order to automate tasks like finding phrases in books and using different regex for different things. Programming can be useful in any field I feel. However right now I am putting together a cookbook and I'm wondering a couple of things. The first I can probably figure out myself 'How do I write so my print function so it isn't one endless line?' and also, my main question is 'how would I format this code to print out all recipes with the word 'egg' or 'eggs' when it is input?'

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

Assistance is greatly appreciated, as I have a girlfriend with a need for a recipe book and I said id make a better one than she could buy lol

Thank you in advance

1 Answers1

1

You could alter your code with the regex module 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
Jan
  • 42,290
  • 8
  • 54
  • 79
  • okay, so I just fill in the print function with all of the recipes that have eggs included. so if I wanted to make an expression for 'breakfast' or 'breakfasts' I would use something like (rx = r'\breakfasts?\b') no parenthesis ? – AutomateStuff Feb 19 '16 at 17:45
  • breakfast would be `rx = r'breakfasts?'` - parentheses are only needed when you want to capture aka save something. It's better to somewhat make yourself familiar with the regex language. – Jan Feb 19 '16 at 17:58
  • De rien je vous en prie :) – Jan Feb 19 '16 at 18:02