9. Something of Value!


I got the answer of the exercise with the help from forum, but I want to know why that my code is wrong. Why "total" finally is 0. Thanks!!


Hi, @18youngshu ,

In your loop, you are assigning an entirely new value to total instead of adding prices[key] * stock[key] to the existing value of total. The problem is here ...

total = prices[key] * stock[key]

You can fix the problem by using the += operator instead of =.

Something of value program

Thank you that helped me out!


i don't undestand the logic behind it that why we += assignment operator instead of = ..
please can u explain the logic..?


kudos @wizmarco

totale = totale + n
totale = totale + b

Its the same as..

totale += n
totale += b

Its another  way to add.


A post was split to a new topic: I don't get why I got it wrong?


In the instructions it says to "Create a variable called total and set it to zero.". Why does it ask to do so if when added to the statement it creates an error, but when taken out it completes it?



Where did you place the statement in your code? If you post your code, we can help you figure out what the problem is.


There we go. I added the total = 0 and that pops up, but if I delete it. It works.


@xfearlessangelx ,

Since your code is not formatted, we cannot be sure of what is inside the loop. In any case, the statement ...

total = 0

... needs to be placed before the loop.

The reason the code does not crash when you place it after the loop is that Codecademy remembers variables from previous submissions of your code, unless you refresh the page. So, while debugging the code for an exercise, you should submit the code, then read any messages and output that appear, refresh the page, then edit the code again, etc.


Ahh adding the variable before the loop does work. That actually helps a lot. I misunderstood and thought I was suppose to put it after the loop.


That was very helpful, thank you for helping with that. Cheers.