Something of Value


I need help because I can’t figure out how to print the total thank you for the help.
Heres the code:

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

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]
"total" == 0
for key in prices:
    print prices[key] * stock[key]
    print total


well, first you want to create a variable total and set it 0:

total = 0

nothing weird about this, right? Then you want to add the price * stock of each item to the total price:

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

then, after the for loop is finished, you want to print the total. If you need further help, post an updated version of your code



for key in prices:
    total = prices[key] * stock[key]
    print("{0} * {1} = {2}".format( prices[key], stock[key], total) )
    print "total: "+str(total)


Thank you for the help. I used this, and it worked:

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

Question though: where you used [key] and I used [value], both technically components of a Python dictionary, right? In the loop, Python does not differentiate between calling a key vs a value, correct? It in this case multiplied the values together because they are integers (prices and stock amounts) that can be multiplied, and you can’t multiply strings (the actual keys). Is that correct?


No, it not correct. You can use any word (key, value or even something else, like cost. The trick is here:

for value in prices:

value now holds the key (banana, apple, orange and so on), so you can do:

for value in prices:
    print value

which will print the keys, if you use the bracket notation:

for value in prices:
    print prices[value]

you use the key (called value, confusing) to access the associated value (so prices[‘banana’] is 4. Does that make sense?


Yep, perfect, thanks!



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

Each dictionary ( also called object )
has one or more key-value-pair’s ( also called properties)
which are separated from each-other by a comma-,

The prices dictionary / object
has 4 properties
a banana property with property-key banana and its associated number Value 4
an apple property with property-key apple and its associated number Value 2
an orange property with property-key orange and its associated number Value 1.5
a pear property with property-key pear and its associated number Value 3

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for x in prices:
will lead to 4 iteration’s
iteration-1 var x = “banana”;
iteration-2 var x = “apple”;
iteration-3 var x = “orange”;
iteration-4 var x = “pear”;
As they want you to display the the associated VALUE of the propery-key you would use
print prices[x]

If they wanted you to _Display the property-key and NOT it’s associated VALUE
you would use
print x

As the stock dictionary / object uses the same property-key’s
you can also use
print stock[x]


Hi, sorry for the late reply it worked perfectly thanks