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.
Latihan ini adalah bagian dari kursus
Introduction to Python for Developers
Petunjuk latihan
- 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.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# 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)