Something of Value


#1

https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/1/4?curriculum_id=4f89dab3d788890003000096

Error message: " it looks like your code did not print the correct total.

Please see below, i am unable to print the total. See my code below, what is missing.


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]
for total in prices:
    total = 0
    total += prices[key] * stock[key]
    print total


#3

Well I had nearly the same problem then I realized 'total = 0' should be out of for loop and I guess there should be only on for loop like this:
total = 0
for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
print prices[key] * stock[key]
total = total + prices[key] * stock[key]
print total

I hope that works for you!


#4

I essentially did something similar I just set the total prior to the "for" statement.

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}
total = 0
for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
total = total + prices[key] * stock[key]
#
print total


#5

thanks, this worked and my code is printing properly


#6

I just don't understand this line:

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

if you leave it just:

total = prices[key] * stock[key] # my logic says variable total gets multiplication of keys of prices and stock

Can somebody help me understand why there's extra total in this statement ?

Would be really grateful ....


#7

You are adding the result of:

prices[key] * stock[key]

to "total" each time the loop runs. If you just run:

total = prices[key] * stock[key]

you will replace the result of the last item with the current.

Example:

We know that total is equal to zero (because we haven't added anything yet), a value set outside of the loop. For the first loop, the total price is as follows:

total = 0 + 4 * 6

where 4 is the price for banana, and 6 is the stock of banana

So now, total is equal to 24. Now, for the second loop, the total price is as follows:

total = 24 + 2 * 0

where 24 is the previous total, 2 is the price of apples, and 0 is the number of apples in stock

The new total is 24. But, if you were just to say total = 2 * 0, the new total would be wrong, because it would be zero. We are not trying to replace the total with each loop, but add to it for a grand total of all items.

Therefore, we write:

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

or total+= prices[key] * stock[key] for short. Think of it as the current total = previously calculated total + (current item price * current item stock).

I hope that helps. Feel free to ask for further clarification.


#8

Hi thanks so much for taking time to explain me all of that. Appreciate it very much. Well ... I gotta read it carefully couple of times to help my brain to have a grasp on it.

Thank you ....


#9

It's a different concept. After doing a few exercises, you'll get used to the logic.

Sometimes it helps to see the difference visually, as you run the code. The following is done in JavaScript as opposed to Python, but you should be able to see the different results on the bottom right that will give you more insight as to why adding "total" to itself matters.

JSFiddle URL: https://jsfiddle.net/missydecoded/ydv96fL0/

Cheers!


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.