2016-02-19 70 views
2

我正在閱讀一些關於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做一個更好的人比她買笑

預先感謝您

+0

開始使用Python ['re'模塊文檔(https://docs.python.org/2/library/re.html)。 – glibdud

+0

還指出,在用「」「三引號」「」(或'''三引號''')包圍的python字符串可以跨越多個列。 – akraf

+0

ahhh,解決了多列問題解決問題。謝謝。 – AutomateStuff

回答

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 
+0

好吧,所以我只是用所有包含雞蛋的食譜填寫打印功能。所以如果我想爲'早餐'或'早餐'做一個表達式,我會用(rx = r'\ breakfasts?\ b')沒有括號? – AutomateStuff

+0

早餐應該是'rx = r'breakfasts?' - 只有當你想捕捉又名保存某些東西時才需要括號。最好稍微讓自己熟悉正則表達式語言。 – Jan

+0

De rien je vous en prie :) – Jan