Don't worry, I am here to help you.
In this exercise, you want to calculate a total price of products in the shop / basket.
The price of every product is stored in
prices list and number of products in
You created a loop that iterates through every product. Expression
prices[key] * stock[key] calculates total price of product
Let's execute your loop line by line:
for key in prices:
total = prices[key] * stock[key]
key = "orange"
prices[key] = 1.5
stock[key] = 32
total = 1.5 * 32 = 48.0
key = "pear"
prices[key] = 3
stock[key] = 15
total = 3 * 15 = 45
key = "banana"
prices[key] = 4
stock[key] = 6
total = 4 * 6 = 24
key = "apple"
prices[key] = 2
stock[key] = 0
total = 2 * 0 = 0
Do you see a problem? Your
total variable does not store price of all elements. It only represents price of the last element, because with every iteration you overwrite previous value.