Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Example Recipe] #<- This is the name of the recipe
- 10 #<- This is how many people the recipe servers
- blueberries #<- Each ingredient's name is here
- 10.0 #<- This is the quantity of blueberries
- units #<- This is the units of quantity of blueberries
- strawberries
- 5.0
- kilograms
- [End of recipe] #<- This indicates the end of example recipe
- def input_func(): #defines the input_function function
- name = str(input("What is the name of the recipe?"))
- people = input("How many people are served by the recipe?")
- f = 0 #f stands for finished, and is used as a quick variable for a while loop
- ingredients = [] #defines ingredients as an empty list
- quantity = [] #defines quantity as an empty list
- units = [] #defines units as an empty list
- num_ing = 0 #sets the number of ingredients, which at the start is obviously 0
- while f == 0: #as stated earlier, f is used as a quick variable for the while loop
- ingredients.append(input ("What is the name of the ingredient?")) #asks end user to input the name of an ingredient
- num_ing = num_ing + 1 #sets the number of ingredients to the previous numberr + 1, so that the program can keep track of number of ingredients
- print(num_ing) #THIS IS FOR TESTING PURPOSES
- quantity.append (float((input("What is the quantity of the ingredient? (numbers only)")))) #asks end user to input the quantity as a integer e.g. 500
- units.append (str(input("What units is the ingredient measuered in?"))) #asks end user to input the units of the previous integer e.g. grams
- choice = input("Are there any more ingredients? (Enter no or yes)") #asks the user if there are any more ingredients to input
- if choice == "no": #if the user says no, the loop is broken in order to write to the file
- break
- else:
- continue #if the user says anything else, the while loop continues
- file = open("recipes.txt", "a")
- file.write("["+str(name)+"]")
- file.write("n")
- file.write(str(people))
- file.write("n")
- file.write("n")
- for i in range(num_ing):
- file.write(ingredients[i])
- file.write("n")
- file.write(str((quantity[i])))
- file.write("n")
- file.write(str(units[i]))
- file.write("n")
- file.write("n")
- file.write("[End of recipe]")
- file.close() #closes the file
- initial_menu()
- def output_func():
- import string
- search_name = str(input("What is the name of the recipe you wish to retrieve?"))
- for line in open("recipes.txt"):
- if "["+search_name+"]" in line:
- print("The recipe "+search_name+" exists") #prints that the recipe exists
- def input_func(): #defines the input_function function
- ...
- return {
- 'name': name,
- 'people': people,
- 'ingredients': ingredients,
- 'quantity': quantity,
- 'units': units,
- 'num_ing': num_ing,
- }
- def open_recipes(path):
- try:
- with open(path) as stream:
- return json.loads(stream.read())
- except FileNotFoundError:
- # start a new database
- return {}
- def save_recipes(path, recipes):
- with open(path, 'w') as stream:
- stream.write(json.dumps(recipes, indent=2))
- # open the recipe database
- recipes = open_recipes('recipes.json')
- # start a new recipe
- recipe = input_func()
- name = recipe['name']
- # check if the recipe already exists
- if name not in recipes:
- # store the recipe in the database
- recipes[name] = recipe
- # save the database
- save_recipes('recipes.json', recipes)
- else:
- print('ERROR: recipe already exists:', name)
- # rename recipe...
- ...
- # find an existing recipe
- search_name = str(input("What is the name of the recipe you wish to retrieve?"))
- if search_name in recipes:
- # fetch the recipe from the database
- recipe = recipes[search_name]
- # display the recipe...
- else:
- print('ERROR: could not find recipe:', search_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement