Something of Value Help Please


When saved its telling me I'm not getting the correct count (which is probably true since I'm getitng 0).

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 x in prices:
    x * stock[x]
    total = x * stock[x]
print total


Do you know what steps it's supposed to be doing to get the right results? (In other words, could you do it manually?)
If so, then you can add prints in your code and start making observations about what it's doing. Compare to what you've decided that it should be doing, and fix the differences.


Well I realize that it wants me simply multiply the stock by the price of the stock.

for x in prices:

prices[x] * stock[x]

I had tried that and still had gotten the same result. I would figure that that would at least get me the result I wanted but no dice so far.

But I'll keep trying. Thanks!


If you've identified which line isn't behaving as you want then you're in a pretty good position to use a search engine to find out how to do whatever it is you want that line to do.
I half suspect that you haven't thought it through in sufficient detail though, make sure that you're identifying what it's doing differently from what you expect, that difference says what you need to change


I did manage to figure it out.

I had to create a separate value inside the loop to print and to use that to add to my total

for x in prices:
    total_value = prices[x] * stock[x]
    print total_value
    total += total_value
print total

That did the trick for me. It makes sense it's just that I didn't think at first i'd need to create a separate variable inside my for loop


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