On Sat, May 18, 2013 at 2:19 PM, Bradley Wright <bradley.wright....@gmail.com> wrote: > Confusing subject for a confusing problem (to a novice like me of course!) > Thx for the help in advance folks > > I have (2) dictionaries: > > prices = { > "banana": 4, > "apple": 2, > "orange": 1.5, > "pear": 3 > } > > stock = { > "banana": 6, > "apple": 0, > "orange": 32, > "pear": 15 > } > > Here's my instructions: > > consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!)
Let me reword your problem a little, maybe it'll be a bit clearer. You're trying to calculate the total value of all stock on hand, eg for insurance purposes. That's not 24 bananas, that's $24 of bananas. And the part you want now is to get the total value of your entire stock. Great! You're very close to there... > HERES MY CODE: > > for key in prices: > print prices[key]*stock[key] > > ISSUE: > I need to find a way to add all of those together...any pointers? ... you just need to accumulate a sum. Since this is almost certainly homework, I won't give you the answer, but here are a few pointers: * You'll need a single variable (I use the term sloppily, Python doesn't actually have variables per se) which will collect the final total. * Inside your loop, you're currently printing out an int/float with the value of the current item. Just add it onto your accumulator. * Python will happily work with integers and floats together, so you can just do what's obvious and it'll work. See where that takes you. Have fun! :) ChrisA -- http://mail.python.org/mailman/listinfo/python-list