我正在制作食谱书,目前我有能力创建食谱,但现在我开始构建搜索和显示存储食谱的模块。返回文本文件的内容并打印不同的值
目前,我与沿线的内容为.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)
这个问题是...非常广泛 - 我不确定你在找什么。 – Ajean 2014-11-05 20:57:41
对不起,我这里是新来的。但是,尽管如此,欣赏的反应。我试图找到允许配方存储在一个单独的文本文件中,然后在搜索时调用,但返回不同的成分取决于服务的人数。所以120克这个服务12人。但我服务24,因此我想它打印240克这种成分。我想向用户返回不同的值。希望这可以解决问题。 – WJPreston 2014-11-06 18:34:35