Created
March 23, 2021 09:02
-
-
Save AnisahTiaraPratiwi/3f55a07acf146a723e51e0acae1e0e01 to your computer and use it in GitHub Desktop.
The add_prices function returns the total price of all of the groceries in the dictionary. Fill in the blanks to complete this function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_prices(basket): | |
# Initialize the variable that will be used for the calculation | |
total = 0 | |
# Iterate through the dictionary items | |
for item, price in basket.items(): | |
# Add each price to the total calculation | |
# Hint: how do you access the values of | |
# dictionary items? | |
total += price | |
# Limit the return value to 2 decimal places | |
return round(total, 2) | |
groceries = {"bananas": 1.56, "apples": 2.50, "oranges": 0.99, "bread": 4.59, | |
"coffee": 6.99, "milk": 3.39, "eggs": 2.98, "cheese": 5.44} | |
print(add_prices(groceries)) # Should print 28.44 |
def add_prices(basket):
# Initialize the variable that will be used for the calculation
total = 0
# Iterate through the dictionary items
for price in basket.values():
# Add each price to the total calculation
# Hint: how do you access the values of
# dictionary items?
total += price
# Limit the return value to 2 decimal places
return round(total, 2)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
groceries = {"bananas": 1.56, "apples": 2.50, "oranges": 0.99, "bread": 4.59, "coffee": 6.99, "milk": 3.39, "eggs": 2.98, "cheese": 5.44}
def add_prices(basket):
total = 0
for groceries, prices in basket.items():
total += prices
return round(total, 2)
add_prices(groceries)