Something of Value - A Day at the Supermarket


#1

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

I have use a for loop to multiply prices[food] and stock[food].
But how do I add up all the multiplicative results when they are all separated?


#2

It would have been nice we see your for loop for that.

Be it as it may, to add up the multiplicative results, you could:

total = 0
for key in prices:
    total += prices[key]*stock[key]
    print total

#3

Thank you. I was wondering what += means.


#4

It's simply after you do prices[key]*stock[key], it adds it to the initial value of total which in the first instance is 0 then still equates that resultant value to have a new total.

Hope this helps! :slight_smile: