Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'returned.': 0.07; 'title.': 0.09; 'def': 0.12; "(i'm": 0.16; '1.5,': 0.16; '32,': 0.16; 'called,': 0.16; 'food:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'guessing': 0.16; 'interpreter,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'script,': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'bit': 0.19; 'result.': 0.19; 'written': 0.21; 'code,': 0.22; 'header:User- Agent:1': 0.23; 'adds': 0.24; "haven't": 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; "i'm": 0.30; "skip:' 10": 0.31; 'anyone': 0.31; 'know.': 0.32; 'thanks!': 0.32; 'run': 0.32; 'trouble': 0.34; 'could': 0.34; 'received:84': 0.35; 'but': 0.35; 'list': 0.37; 'step': 0.37; 'checks': 0.38; 'to:addr:python- list': 0.38; 'does': 0.39; 'stock': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'prices': 0.60; 'mentioned': 0.61; 'matter': 0.61; 'first': 0.61; "you've": 0.63; 'total': 0.65; "'food'": 0.84; 'total,': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=Pb1xh0Rd c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=Vhvw94NMJWsA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=2Ih22D-n-gnWsDrzQYoA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Tue, 20 Jan 2015 00:27:53 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Concerning Dictionaries and += in Python 2.x References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421713863 news.xs4all.nl 2842 [2001:888:2000:d::a6]:57821 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84039 On 2015-01-20 00:12, Luke Tomaneng 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! > Work through it a step at a time. You haven't said what 'food' is when 'compute_bill is called, but I'm guessing that the first item it checks is "banana". stock["banana"] == 6, so it adds prices["banana"] to total, subtracts 1 from stock["banana"], and then returns the total, 4, because that's what you've told it to do.