I have a nested dictionary that stores recipes that a user inputs. Within the nested dictionary I have a list. I would like to find a way to neatly display the contents of the list as I am displaying the contents of the nested dictionary. Please see the code samples below as well as the output I would like to receive.
def build_ingredients(self):
"""Creates a list of ingredients based off user input"""
next_ingredient = ''
while True:
next_ingredient = self.ask_input("Please enter an ingredient for this recipe or type done to continue ")
if next_ingredient.lower() == 'done':
break
self.ingredients.append(next_ingredient)
def add_recipe(self):
"""Adding a recipie to the recipie book"""
recipie_name = self.ask_input("Please enter the name of the recipie that you are adding: ").title()
category = self.ask_input("Please enter the category you would like this recipie to be a part of: ").title()
preptime = self.ask_input("Please enter the amount of time it will take to prepare this recipe: ").title()
cooktime = self.ask_input("Please enter the amount of time in minutes it will take to complete this recipie (please include minutes or hours): ").title()
self.build_ingredients()
instructions = self.ask_input("Please list out the instructions to making this recipie: ").title()
print('____________________________________________________________________________________________________________')
if self.recipes[str(1)] == {'Recipe Name' : '' , 'Category' : '' ,'Prep Time' : '' , 'Cooktime' : '' , 'Ingredients' : '' , 'Instructions' : ''}:
self.recipes[str(1)] = {'Recipe Name' : f'{recipie_name}', 'Category' : f'{category}' , 'Preptime': f'{preptime}', 'Cooktime' : f'{cooktime}' , 'Ingredients' : f'{self.ingredients}' , 'Instructions' : f'{instructions}'}
self.save_data()
else:
self.recipes[str(len(self.recipes) + 1)] = {'Recipe Name' : f'{recipie_name}', 'Category' : f'{category}' ,'Preptime': f'{preptime}', 'Cooktime' : f'{cooktime}' , 'Ingredients' : f'{self.ingredients}' , 'Instructions' : f'{instructions}'}
self.save_data()
def view_selected_recipe(self):
"""once the user determines what recipe they would like to view, display it in a neat format"""
while True:
try:
recipe_choice = self.ask_input("\nPlease enter the number that corresponds to the recipe you would like to view: ")
if recipe_choice in self.number_list:
break
print("Please enter a number")
except Exception as e:
print(e)
print("\n")
for key, value in self.recipes[str(recipe_choice)].items():
if f"{key}" == 'Ingredients':
for item in self.ingredients:
print(f"\t{item}")
print(f"\t{key} : {value}")
The output that I am seeing is:
Recipe Name : Pbj
Category : Snack
Preptime : 1
Cooktime : 1
Ingredients : ['pb', 'j', 'bread']
Instructions : Spread And Eat
The output that I would like to see is:
Recipe Name : Pbj
Category : Snack
Preptime : 1
Cooktime : 1
Ingredients:
pb
j
bread
Instructions: spread and eat