from contents import pantry chicken_quantity = pantry.setdefault("chicken", 0) print(f"chicken: {chicken_quantity}") beans_quantity = pantry.setdefault("beans", 0) print(f"beans: {beans_quantity}") ketchup_quantity = pantry.get("ketchup", 0) print(f"ketchup: {ketchup_quantity}") # default can be a string value as well z_quantity = pantry.setdefault("zucchini", "eight") print() print("'pantry'now contains...") for key, value in sorted(pantry.items()): print(key, value) # setdefault will check and see if the key is in the dict, and add # that key if it does not exist # get will not
for index, key in enumerate(recipes): display_dict[str(index + 1)] = key shopping_list = {} #empty dictionary while True: print("Please choose your recipe") print("-------------------------") for key, value in display_dict.items(): print(f"{key} - {value}") choice = input(": ") if choice =="0": break elif choice in display_dict: selected_item = display_dict[choice] print(f"You have selected {selected_item}") print("checking ingredients ...") ingredients = recipes[selected_item] print(ingredients) for food_item, required_quantity in ingredients.items(): quantity_in_pantry = pantry.get(food_item, 0) if required_quantity <= quantity_in_pantry: print(f"\t{food_item} in pantry") else: quanity_to_buy = required_quantity - quantity_in_pantry print(f"\tYou need to buy {quanity_to_buy} of {food_item}") add_shopping_item(shopping_list, food_item, quanity_to_buy) for things in shopping_list.items(): print(things)
display_dict[str(index + 1)] = key while True: print("Please choose your recipe") print("-------------------------") for key, value in display_dict.items(): print(f"{key} - {value}") choice = input(": ") if choice =="0": break elif choice in display_dict: selected_item = display_dict[choice] print(f"You have selected {selected_item}") print("checking ingredients ...") ingredients = recipes[selected_item] print(ingredients) shopping_list = {} #emplty shopping list for food_item, required_quantity in ingredients.items(): quanitiy_in_pantry = pantry.get(food_item, 0) if required_quantity <= quanitiy_in_pantry: print(f"\t{food_item} in pantry") else: quanity_to_buy = required_quantity - quanitiy_in_pantry print(f"\tYou need to buy {quanity_to_buy} of {food_item}") shopping_list[food_item] = quanity_to_buy print(f"You need to buy: ") print(shopping_list)