Calculating Total Within a Loop

I tried in the same loop,but I got the total 0

Hi @gangapuli,

Could you please post a link to the exercise you’re on and a copy of your code?

total = 0

for food in prices:

print(prices[food] * stock[food])

total = total + prices[food] * stock[food]

print(total)

why does the total have to be indented to the loop indentation for the answer to appear. If i put it on the baseline i get total = 0 and it doesnt work. But when i indent it to the loop level then i get 117 as a float answer.

I dont think it shows in this comment but the print(price[food] * stock[food]) and the total = total + … should be indented in my code

Indentation can be preserved with formatting, you should take a look at this post.

total = 0

for food in prices:

  print(prices[food] * stock[food])

  total = total + prices[food] * stock[food]

print(total)

For a more specific answer I’d need to know what stock and prices are, dictionary/list/2d lists.
Generally, food is a temporary variable for the loop and the total equation is meant to iterate through your list so each item is added.

total = 0 prices = [1, 2, 3] for food in prices: print(f"{food}") print(f"{food}")