Why is this not printing?


#1

This is supposed to take the number for each item in prices and multiply it with the number for each item in stock to get the total cost. It doesn't work though and I can't figure out why.

total = 0
for food in prices:
total = prices[food] * stock[food]
return total
print total


#2

You're resetting the value of total each time you run through the for loop.

So, consider what happens the first time. You set the value correctly. But then when you run back through the second time, if total was 10 on the first time around, the next time it doesn't keep 10 and add more. Instead, if the value from the second item is 12, the total is set to 12 instead of 22.

What you need to do is add to the total.


#3

Thanks! That makes sense now. It was returning 0 and that's because the last number was 0. I added total + and it works :slightly_smiling: