This community-built FAQ covers the “Something of Value” exercise in Codecademy’s lessons on Python.
FAQs for the Codecademy Python exercise Something of Value:
None of the above? Find out where to ask other questions here!
This community-built FAQ covers the “Something of Value” exercise in Codecademy’s lessons on Python.
None of the above? Find out where to ask other questions here!
The following are links to additional questions that our community has asked about this exercise:
Not seeing your question? It may still have been asked before – try () in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (
).
again, your interpreter is too simple.
the solution “total = total + prices[food] * stock[food]” is the same as
total += prices[ x ] * stock[ x ] <-- i have to place a space before and after the “x” otherwise it becomes a check mark!
but the interpreter rejects it with an error!
Why does this work:
total = total + prices[fruit] * stock[fruit]
print total
#the sum of prices multiplied by the stock and returns 117.0
but this does not work and returns 0
total2 = prices[fruit] * stock[fruit]
Print total + total2
#new variable total2 should equal 117.0
#new variable total2 + total should equal 117.0
Why doesn’t my code work?
Your code doesn’t work because food is not a key. You should have switch the ‘food’ with key because its the key of the price and stock.
then after that you print the variable
Hey bit,
If you referred the earlier exercises, there was a similar example on this.
Your current code will print out the total price of each fruit separately, but it will not collate them under one function. You also did not make use of total in any of the code, and it still returns a value of 0 at the end.
The right way to do it is to create a def function(n) which encapsulates the looping function, where n will be substituted for the stock list later on. After you do the looping function (for food in prices), you need to give the ‘total’ the value of (price * food), and then return this function for it to loop again, to be added on by the (price * food) of the next list entry which is apple. Finally, print function(prices), which substitutes n for the prices list, where the function is able to refer to this list for its processes.
Hope this helps!
It looks like all works correctly in those code.
total = 0
for num in prices:
sym = prices[num] * stock[num]
total = total + sym
print "Tottal: %s" % total
why doesnt this work
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:
total += prices[i] * stock[i]
print key
print “price: %s” % prices[key]
print “stock: %s” % stock[key]
print total
you wrote i
instead of key
It should be
total += prices[key] * stock[key]