Path: csiph.com!feeder.erje.net!2.us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Simple exercise Date: Thu, 10 Mar 2016 13:20:46 -0500 Lines: 63 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de B6RryI/Fb1Ul74KY5rKM7Qz+zWdLzkdrSG1ZbwVGoelg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; 'sys': 0.05; '__name__': 0.07; 'lines.': 0.07; 'answer?': 0.09; 'collections': 0.09; 'here?': 0.09; 'incorrect': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; 'output': 0.13; "'__main__':": 0.16; 'ordereddict': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'input': 0.18; 'first,': 0.20; 'am,': 0.23; 'seems': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'skip:# 10': 0.27; 'cat': 0.29; "i'm": 0.30; 'print': 0.30; 'code': 0.30; 'skip:. 10': 0.32; 'subject:Simple': 0.33; 'returning': 0.35; 'skip:i 20': 0.36; 'apple': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'wrong': 0.38; 'goes': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:96': 0.63; 'within': 0.64; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104537 On 3/10/2016 4:02 AM, Rodrick Brown wrote: > From the following input > > 9 > BANANA FRIES 12 > POTATO CHIPS 30 > APPLE JUICE 10 > CANDY 5 > APPLE JUICE 10 > CANDY 5 > CANDY 5 > CANDY 5 > POTATO CHIPS 30 > > I'm expecting the following output > BANANA FRIES 12 > POTATO CHIPS 60 > APPLE JUICE 20 > CANDY 20 > > However my code seems be returning incorrect value Learn to debug. The incorrect value is the one for candy. First, reduce you input to the candy lines. Still get wrong answer? Then print the value in od within the loop after each calculation to see when and how it goes wrong. > #!/usr/bin/env python3 > > import sys > import re > from collections import OrderedDict > > if __name__ == '__main__': > > od = OrderedDict() > recs = int(input()) > > for _ in range(recs): > file_input = sys.stdin.readline().strip() > m = re.search(r"(\w.+)\s+(\d+)", file_input) > > if m: > if m.group(1) not in od.keys(): > od[m.group(1)] = int(m.group(2)) > else: > od[m.group(1)] += int(od.get(m.group(1),0)) > for k,v in od.items(): > print(k,v) > > What's really going on here? > > $ cat groceries.txt | ./groceries.py > BANANA FRIES 12 > POTATO CHIPS 60 > APPLE JUICE 20 > CANDY 40 > -- Terry Jan Reedy