Trying to iterate over floats in a dictionary

Viewed 3131

I am trying to complete this function but to no avail because the dictionary contains float values that I am trying to total up.

Here's the code I have so far:

def add_prices(basket):
    # Initialize the variable that will be used for the calculation
    total = 0
    # Iterate through the dictionary items
    for groceries, prices in basket.items():
        # Add each price to the total calculation
        for price in prices:
        # Hint: how do you access the values of
        # dictionary items?
            total += price.values()
    # 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

I'm completely dumbfounded tbh and need help, as tried typecasting to directly assigning the price to the values.

5 Answers

You don't have to iterate over prices, as they are already float values. Hence the error message that floats are not iterable.

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)
#28.44

As also mentioned in the comments, it might be easier to change your function into a one-liner so you can avoid the for loop

def add_prices(basket):
    return round(sum(basket.values()), 2)
add_prices(groceries)
#28.44

You are unnecessarily doing another loop. You can directly add the price. Read about dict.keys(), dict.values(), dict.items.

It should be like

def add_prices(basket):
    # Initialize the variable that will be used for the calculation
    total = 0
    # Iterate through the dictionary items
    for groceries, prices in basket.items():
        total += prices
        # Add each price to the total calculation
        #for price in prices:
        # Hint: how do you access the values of
        # dictionary items?
            #total += price.values()
    # Limit the return value to 2 decimal places
    return round(total, 2) 
print(add_prices(groceries)) 
>>> 28.44

I don't know why you did the second loop inside the main one when you can directly do this:

def add_prices(basket):
   # Initialize the variable that will be used for the calculation
   total = 0
   # Iterate through the dictionary items
   for groceries, prices in basket.items():
      total += prices
   # Limit the return value to 2 decimal places
   return round(total, 2) 
def add_prices(basket):
    # Initialize the variable that will be used for the calculation
    total = 0
    # Iterate through the dictionary items
    for  items in basket.values():
        
        total += items
    # 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

Ways to Iterate Over a Dictionary

Suppose we have a dictionary

groceries = {"banana": 0.99, "milk": 3.99, "eggs": 2.99}

Here's how we can iterate over it

  1. Iterate over the keys
for item in groceries:
    print(item)

Result:

banana
milk
eggs
  1. Iterate over the values
for price in groceries.values():
    print(price)

Result

0.99
3.99
2.99
  1. Iterate over the key, value pairs
for item, price in groceries.items():
    print(item, price)

Result

banana, 0.99
milk, 3.99
eggs, 2.99

When to Use Each Iteration Method

When I store data in a dictionary, I typically do it in such a way that it is most useful to iterate over the (key, value) pairs using dict.items. In this case both the key and the value provide meaningful information and I want to use both during the iteration.

Groceries make sense as a dictionary because it seems reasonable to need both the key, i.e. the grocery item such as banana, and the value, i.e. the cost of the bananas. In this question, though you're just totaling the prices, regardless of what the item is. Thus, you don't need the keys, just the values.

If you just wanted to know the items on the grocery list then you would iterate over just the keys.

Related