In article <6012d69f-b65e-4d65-90c4-f04876853...@googlegroups.com>, 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: Hmmm, homework for a class? > consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!) I suspect what you're trying to say is that bananas cost BTC 4 each, and since you've got 6 bananas, you've got BTC 24 worth of bananas, yes? And now you want to find the total value of your fruit supply? >> HERES MY CODE: > > for key in prices: > print prices[key]*stock[key] > > HERES THE OUTPUT: > > 48.0 > 45 > 24 > 0 So far, so good. A couple of things you may have noticed along the way: 1) Your orange unit price was a float, so the total value of all your oranges is a float as well. That's how math works in Python. 2) The keys are presented in random order. To make the output easier to interpret, you might want to do: print key, prices[key]*stock[key] > ISSUE: > I need to find a way to add all of those together...any pointers? The most straight-forward way would be something like: total = 0 for key in prices: fruit_subtotal = prices[key]*stock[key] total += fruit_subtotal print key, fruit_subtotal print total There are better ways to do this in Python, but start like this and get that to work. -- http://mail.python.org/mailman/listinfo/python-list