Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.064 X-Spam-Evidence: '*H*': 0.88; '*S*': 0.01; 'variables': 0.07; 'advance': 0.07; 'integers': 0.09; 'issue:': 0.09; 'item.': 0.09; 'thx': 0.09; 'python': 0.11; '1.5,': 0.16; '32,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'great!': 0.16; 'happily': 0.16; 'subject:Two': 0.16; 'there...': 0.16; 'folks': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'trying': 0.19; 'print': 0.22; 'certainly': 0.24; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'wright': 0.31; 'maybe': 0.34; 'problem': 0.35; '(2)': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'stock': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'prices': 0.60; 'entire': 0.61; "you're": 0.61; 'you.': 0.62; "you'll": 0.62; 'term': 0.63; 'sum': 0.64; 'total': 0.65; 'here': 0.66; 'close': 0.67; 'subject': 0.69; 'obvious': 0.74; 'confusing': 0.84; 'prices:': 0.84; 'stock.': 0.84; 'sum.': 0.84; 'together,': 0.84; 'novice': 0.91; 'fun!': 0.93; 'hand,': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=jPI1T6kzi2cbwembUeA7z0U2qsYxOhMRMMoQwVzsQt4=; b=ibGu8eaueamr3u5fXCl1X6kwge56q7hzCRFLtXwGvRP5BNqNizf5/xPP8OT9dHGruh g6nbXUFvbKsGT2d9JWC/BO1D2kkJwLI1LemR5mVQ/Tfcx+1FHHoJKR8iJYHGiZB3aX9/ Qd86GBGg90yBCWnEaYUo3uP36q0DIe/T7LhbR/rkJcAwYQht+yq0AB+0j8n7K0MkuOBT oVX64ITiBKWasJeDRkuU4vw53A53ROcnPxJKwUvki7qrzIe2xNXVzVizytUquf78IEQG PIc762/Hpe6NxTFeqrWgH4pO/V9mVsKsGQmMM061sseQpTAsbwCXv9V0eW/g2mGqmz3F ky2A== MIME-Version: 1.0 X-Received: by 10.220.109.66 with SMTP id i2mr1552226vcp.51.1368853830860; Fri, 17 May 2013 22:10:30 -0700 (PDT) In-Reply-To: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> References: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> Date: Sat, 18 May 2013 15:10:30 +1000 Subject: Re: Two Dictionaries and a Sum! From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368853832 news.xs4all.nl 15977 [2001:888:2000:d::a6]:57306 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45495 On Sat, May 18, 2013 at 2:19 PM, Bradley Wright 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