Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. [Example Recipe] #<- This is the name of the recipe
  2. 10 #<- This is how many people the recipe servers
  3.  
  4. blueberries #<- Each ingredient's name is here
  5. 10.0 #<- This is the quantity of blueberries
  6. units #<- This is the units of quantity of blueberries
  7.  
  8. strawberries
  9. 5.0
  10. kilograms
  11.  
  12. [End of recipe] #<- This indicates the end of example recipe
  13.  
  14. def input_func(): #defines the input_function function
  15. name = str(input("What is the name of the recipe?"))
  16. people = input("How many people are served by the recipe?")
  17. f = 0 #f stands for finished, and is used as a quick variable for a while loop
  18. ingredients = [] #defines ingredients as an empty list
  19. quantity = [] #defines quantity as an empty list
  20. units = [] #defines units as an empty list
  21. num_ing = 0 #sets the number of ingredients, which at the start is obviously 0
  22. while f == 0: #as stated earlier, f is used as a quick variable for the while loop
  23. ingredients.append(input ("What is the name of the ingredient?")) #asks end user to input the name of an ingredient
  24. 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
  25. print(num_ing) #THIS IS FOR TESTING PURPOSES
  26. 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
  27. 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
  28. choice = input("Are there any more ingredients? (Enter no or yes)") #asks the user if there are any more ingredients to input
  29. if choice == "no": #if the user says no, the loop is broken in order to write to the file
  30. break
  31. else:
  32. continue #if the user says anything else, the while loop continues
  33.  
  34. file = open("recipes.txt", "a")
  35. file.write("["+str(name)+"]")
  36. file.write("n")
  37. file.write(str(people))
  38. file.write("n")
  39. file.write("n")
  40. for i in range(num_ing):
  41. file.write(ingredients[i])
  42. file.write("n")
  43. file.write(str((quantity[i])))
  44. file.write("n")
  45. file.write(str(units[i]))
  46. file.write("n")
  47. file.write("n")
  48. file.write("[End of recipe]")
  49. file.close() #closes the file
  50. initial_menu()
  51.  
  52. def output_func():
  53. import string
  54. search_name = str(input("What is the name of the recipe you wish to retrieve?"))
  55. for line in open("recipes.txt"):
  56. if "["+search_name+"]" in line:
  57. print("The recipe "+search_name+" exists") #prints that the recipe exists
  58.  
  59. def input_func(): #defines the input_function function
  60. ...
  61. return {
  62. 'name': name,
  63. 'people': people,
  64. 'ingredients': ingredients,
  65. 'quantity': quantity,
  66. 'units': units,
  67. 'num_ing': num_ing,
  68. }
  69.  
  70. def open_recipes(path):
  71. try:
  72. with open(path) as stream:
  73. return json.loads(stream.read())
  74. except FileNotFoundError:
  75. # start a new database
  76. return {}
  77.  
  78. def save_recipes(path, recipes):
  79. with open(path, 'w') as stream:
  80. stream.write(json.dumps(recipes, indent=2))
  81.  
  82. # open the recipe database
  83. recipes = open_recipes('recipes.json')
  84.  
  85. # start a new recipe
  86. recipe = input_func()
  87.  
  88. name = recipe['name']
  89.  
  90. # check if the recipe already exists
  91. if name not in recipes:
  92. # store the recipe in the database
  93. recipes[name] = recipe
  94. # save the database
  95. save_recipes('recipes.json', recipes)
  96. else:
  97. print('ERROR: recipe already exists:', name)
  98. # rename recipe...
  99.  
  100. ...
  101.  
  102. # find an existing recipe
  103. search_name = str(input("What is the name of the recipe you wish to retrieve?"))
  104.  
  105. if search_name in recipes:
  106. # fetch the recipe from the database
  107. recipe = recipes[search_name]
  108. # display the recipe...
  109. else:
  110. print('ERROR: could not find recipe:', search_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement