Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'returned.': 0.07; 'title.': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; "(i'm": 0.16; '1.5,': 0.16; '32,': 0.16; 'cc:name:python list': 0.16; 'food:': 0.16; 'interpreter,': 0.16; 'script,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'result.': 0.19; 'written': 0.21; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'message-id:@mail.gmail.com': 0.30; 'anyone': 0.31; 'know.': 0.32; 'thanks!': 0.32; 'run': 0.32; 'trouble': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'returning': 0.36; 'list': 0.37; 'pm,': 0.38; 'does': 0.39; 'stock': 0.39; 'sure': 0.39; 'prices': 0.60; 'mentioned': 0.61; 'matter': 0.61; "you're": 0.61; 'first': 0.61; 'total': 0.65; 'to:addr:gmail.com': 0.65; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=Er243kvNOkoz43PhcMdDXQk1NEHEF2tgS7XXWw3RdEQ=; b=HMp/FTlEmGcyDOInppqD1eWSsjNtDWKd/y6rdBDR/ula4NIUqTDLGRISxphcNOZ2Fh jSE0Z5YCEmkmo7CW0yRvNU6Nw1HzoUJZ3forMevtRtbP73BKU680BMQGLjcgm59sWVIG k1E1F4voQagCEIT0kL6Xj/w8izu0IWcOv71HnowUNp0I/UDy4lCktFZEZOYrIwWElVaZ kyf65VCueVjhtxCaMFylVc5mf+Smf7xLcGDOCia4glF+0qtFhOJHjDXK4y0cVE9yErKY dlg8YUnSoYB4D0TR/EPeBoxNm/niXEtR9ockrJGSjvA9szdp1uvUUfPgn7jiMMwAFzUE 6FGg== MIME-Version: 1.0 X-Received: by 10.112.150.194 with SMTP id uk2mr35388700lbb.84.1421715264374; Mon, 19 Jan 2015 16:54:24 -0800 (PST) In-Reply-To: References: Date: Mon, 19 Jan 2015 16:54:24 -0800 Subject: Re: Concerning Dictionaries and += in Python 2.x From: Dan Stromberg To: Luke Tomaneng Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Python List 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421715268 news.xs4all.nl 2884 [2001:888:2000:d::a6]:54543 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84047 On Mon, Jan 19, 2015 at 4:12 PM, Luke Tomaneng wro= te: > I have been having a bit of trouble with the things mentioned in the titl= e. I have written the following script for a Codecademy course: > stock =3D { > "banana": 6, > "apple": 0, > "orange": 32, > "pear": 15 > } > > prices =3D { > "banana": 4, > "apple": 2, > "orange": 1.5, > "pear": 3 > } > > def compute_bill(food): > total =3D 0 > for item in food: > if stock[item] > 0: > total +=3D prices[item] > stock[item] =3D stock[item] - 1 > return total > Whenever I run this script, "4" is returned. It does not seem to matter w= hat in in the list the script is run on. I have tried this on the Codecadem= y interpreter/emulator (I'm not sure which they use) and the repl.it interp= reter, but for the same result. If anyone could find the glitch in my code,= please let me know. Thanks! You're returning total inappropriately - the first time stock[item] is > 0.