I need to call a recursive function to print the contents of the nested dictionary like this on separate lines:
lunch : {"sandwich": 2, "chips": 1, "drink": 1}
dinner : {"ribs": 6, "mac_cheese": 1, "roll": 1, "pie": 1}
I tried the following code but it is just iterating over the keys of the nested dictionary
# recursive function for printing out meal plans
def print_meals(meal_plan):
for key, value in meal_plan.items():
if type(value) is dict:
print_meals(value)
else:
print(key, ":", value)
# create an empty dictionary
meal_plan = {}
# create individual dictionaries for each meal
lunch = {"sandwich": 2, "chips": 1, "drink": 1}
dinner = {"ribs": 6, "mac_cheese": 1, "roll": 1, "pie": 1}
#create a nested dictionary
meal_plan["lunch"] = lunch
meal_plan["dinner"] = dinner
# call recursive function 'print_meals'
print_meals(meal_plan)
meal_plan = {'monday': {'lunch': {...}, 'dinner': {...}}, 'tuesday': {...}}isinstance(value, dict)rather thantype(value) is dict. For instance, ifvalueis a subclass ofdict, thenisinstance(value, dict)will still returnTrue, buttype(value) is dictwill returnFalse.