Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #84047

Re: Concerning Dictionaries and += in Python 2.x

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 <drsalists@gmail.com>
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 <bc9e302a-356c-4223-9c25-6a8003db48e6@googlegroups.com>
References <bc9e302a-356c-4223-9c25-6a8003db48e6@googlegroups.com>
Date Mon, 19 Jan 2015 16:54:24 -0800
Subject Re: Concerning Dictionaries and += in Python 2.x
From Dan Stromberg <drsalists@gmail.com>
To Luke Tomaneng <luketomaneng@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17874.1421715268.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, Jan 19, 2015 at 4:12 PM, Luke Tomaneng <luketomaneng@gmail.com> wrote:
> I have been having a bit of trouble with the things mentioned in the title. I have written the following script for a Codecademy course:
> stock = {
>     "banana": 6,
>     "apple": 0,
>     "orange": 32,
>     "pear": 15
> }
>
> prices = {
>     "banana": 4,
>     "apple": 2,
>     "orange": 1.5,
>     "pear": 3
> }
>
> def compute_bill(food):
>     total = 0
>     for item in food:
>         if stock[item] > 0:
>             total += prices[item]
>             stock[item] = stock[item] - 1
>             return total
> Whenever I run this script, "4" is returned. It does not seem to matter what in in the list the script is run on. I have tried this on the Codecademy interpreter/emulator (I'm not sure which they use) and the repl.it interpreter, 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:12 -0800
  Re: Concerning Dictionaries and += in Python 2.x Chris Angelico <rosuav@gmail.com> - 2015-01-20 11:21 +1100
    Re: Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:31 -0800
  Re: Concerning Dictionaries and += in Python 2.x MRAB <python@mrabarnett.plus.com> - 2015-01-20 00:27 +0000
  Re: Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:34 -0800
    Re: Concerning Dictionaries and += in Python 2.x Chris Angelico <rosuav@gmail.com> - 2015-01-20 11:46 +1100
  Re: Concerning Dictionaries and += in Python 2.x Dan Stromberg <drsalists@gmail.com> - 2015-01-19 16:54 -0800
  Re: Concerning Dictionaries and += in Python 2.x Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-21 05:01 +0000
    Re: Concerning Dictionaries and += in Python 2.x Peter Otten <__peter__@web.de> - 2015-01-21 09:43 +0100
      Re: Concerning Dictionaries and += in Python 2.x Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-21 14:10 +0000
    Re: Concerning Dictionaries and += in Python 2.x Peter Otten <__peter__@web.de> - 2015-01-21 09:59 +0100

csiph-web