2014-11-05 67 views
0

我正在製作食譜書,目前我有能力創建食譜,但現在我開始構建搜索和顯示存儲食譜的模塊。返回文本文件的內容並打印不同的值

目前,我與沿線的內容爲.txt文件:

威廉姆斯特殊配方

主料:

麪包:120克 黃油:1234克

配方服務:12

然後我詢問用戶他們服務的數量,並根據配方的服務數量,我需要將所有配料數量乘以該數量。然後我需要再次用完整的配方打印出來。

我在想如何去實現這個結果,而不是特別要求編碼響應作爲答案,但是我會非常感謝我將如何處理這個任務以及所需的任何特定功能。

到目前爲止,我還包括了我的代碼,我很欣賞這個事實,它現在是非常沒有組織的,並且可能很難理解,但我將它包括在內以供參考。

(我還創建了所有創建的食譜的.txt文件,稍後將作爲向用戶顯示所有食譜的一種方式實施,但目前它只是設置用於搜索。)

#Recipe Task 

import os.path 

def file_(n): 
    if n == "listR" : 
     list_f = open("list_recipes.txt", "a+") 
     list_f.write(new_name + "\n") 
    if n == "oar": #open append read 
     f=open(new_name + ".txt","a+") 
    elif n == "c": #closes file 
     f.close() 


def print_line(x): #ease of printing multiple lines to break up text 
    for c in range(x): 
     print "" 

def new_ingredients(): #adding new ingredients 
    f.write("Ingredients:" + "\n" + "\n") 
    fin_ingredient = False 
    while fin_ingredient != True : 
     input_ingredient = raw_input("New ingredient:" + "\n").lower() 
     split_ingred = input_ingredient.split() 
     if input_ingredient == "stop": #stops asking questions when user types 'stop' 
      fin_ingredient = True 
     else : 
      f.write(split_ingred[0] + ":" + " " + split_ingred[1] + " " + split_ingred[2] + "\n") 

def search_recipe(n): #searching for recipes 
    n = n + ".txt" 
    if os.path.isfile('/Users/wjpreston/Desktop/' + n) == True : 
     print "Recipe Found..." 
     found_recipe = open(n) 
     print found_recipe.read() 
     append_serving = raw_input("Would you like to change the number of people you are serving?" + "\n").lower() 
     if append_serving == "yes" : 
      appended_serving = input("How many would you like to serve?" + "\n") 

      with open(n) as f: #here is my issue - not sure where to go with this!! 
       list_recipe = f.readlines() 

      found_recipe.close() 
     else : 
      print "fail" 



    else: 
     print "No existing recipes under that name have been found." 







print "Welcome to your Recipe Book" 
print_line(3) 

recipe_phase = raw_input("Are you 'creating' a recipe or 'viewing' an existing one?" + "\n").lower() 

if recipe_phase == "creating": 

    new_name = raw_input("Name of Recipe: " + "\n") 
    file_("listR") 
    file_("oar") 
    f.write("------------" + "\n" + new_name + "\n" + "\n") 
    print "Ingrediants required in the format 'ingredient quantity unit' - type 'stop' to end process" 
    new_ingredients() 
    new_num = input("Number serving: ") 
    f.write("\n" + "Recipe Serves: " + str(new_num) + "\n" "\n" + "\n") 
    file_("c") 



elif recipe_phase == "viewing": 
    search = raw_input("Search for recipe: ") 
    search_recipe(search) 
+0

這個問題是...非常廣泛 - 我不確定你在找什麼。 – Ajean 2014-11-05 20:57:41

+0

對不起,我這裏是新來的。但是,儘管如此,欣賞的反應。我試圖找到允許配方存儲在一個單獨的文本文件中,然後在搜索時調用,但返回不同的成分取決於服務的人數。所以120克這個服務12人。但我服務24,因此我想它打印240克這種成分。我想向用戶返回不同的值。希望這可以解決問題。 – WJPreston 2014-11-06 18:34:35

回答

0

我不是處理字符串的專家,但我會按照以下方法處理您的問題:
將每種成分保存在一個新行中。
將加載的字符串拆分「\ n」。
然後處理列表一些for循環,同時創建兩個類型的字典,一個實際數據

e.g. {"bread": 4, "butter": 7} 

,一個是類型各成分的:

e.g. {"bread": grams, "butter": grams} 

的你也應該保存如何許多服務的配方是寫的,也許順序的成分(字典得到存儲在一個隨機的順序):

e.g. ["bread", "butter"] 

之後,你可以詢問客戶他有多少服務,然後最終計算並打印最終結果。

for ing in ing_order: 
     print ing+":", ing_amount[ing]*requested_serves/default_seves, ing_types[ing] 

......呃,你還是有足夠的挑戰,希望我正確理解你的問題。

+0

是的,出於某種原因,在上面包含的配方格式是錯誤的,成分都在自己的線上。儘管非常感謝您的答覆,非常感謝您的努力。我明白你在做什麼,並可能會爲你的方式感謝你。一個問題是,我理解將文本文件的每一行存儲在列表中的方式,但是,如何確定這是否是我需要應用上述規則的配料字段? – WJPreston 2014-11-06 18:40:04

+0

@WJPreston 創建一個名爲'ingredients'的變量。用for循環處理列表。如果當前元素是「Ingredients:」,則將「ingredients」設置爲「True」。然後,也可以在for循環中添加一些「if」語句,例如'if element [:13] ==「Recipe Serves」:process_serves,elif ingredients == True:process_ingredient'。當然有更優雅的方式(也許你找到了一個?),但至少你有一個想法。 – Nearoo 2014-11-06 19:51:04

+0

啊我現在開始瞭解更多,非常感謝你的努力。我是python新手,非常感謝回覆。 – WJPreston 2014-11-07 08:41:13