Looping with a dictionary
Your recipe scaler is coming together! You have a dictionary called recipe that contains ingredient names as keys and their quantities in grams as values for your tomato and basil pasta. Now you need to scale all the quantities by a factor of 2 to serve more guests at your party. You'll loop through the dictionary items and calculate the scaled quantities.
Bu egzersiz
Introduction to Python for Developers
kursunun bir parçasıdırEgzersiz talimatları
- Use a
forloop to iterate through therecipedictionary, usingingredientandqtyas your iterators. - Inside the loop, create a variable called
scaled_qtythat multiplies the original quantity by the scaling factor of 2.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
recipe = {
"fusilli": 500,
"tomatoes": 400,
"basil": 20,
"garlic": 15,
"olive oil": 15,
"salt": 7
}
# Loop through the recipe dictionary items
for ____, ____ in recipe.____:
# Calculate the scaled quantity by multiplying by 2
scaled_qty = qty ____
print(ingredient, ":", scaled_qty, "g")