Appending to a list
You want to test out the shopping list feature for your recipe scaler. Before scaling anything, you want to check which ingredients you don't have enough of in your pantry. You'll loop through the standard recipe dictionary and the items you have in your pantry_stock and add any ingredient to your shopping list where the required amount exceeds what you currently have available.
This exercise is part of the course
Introduction to Python for Developers
Exercise instructions
- Create an empty list called
shopping_listto store ingredients you need to buy. - Loop through the items in the
recipedictionary to access both ingredient names and required quantities. - Inside the loop, check if the
required_qtyis greater than what you have inpantry_stockfor that ingredient. - If you need more of an ingredient, append the ingredient name to
shopping_listusing the.append()method.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create an empty shopping list
shopping_list = ____
# Loop through each ingredient and required quantity
for ingredient, required_qty in recipe.____:
# Check if we need more than what we have
if ____ > pantry_stock[ingredient]:
# Add the ingredient to our shopping list
shopping_list.____
# Display the shopping list
print("Shopping list:", shopping_list)