Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!nuzba.szn.dk!pnx.dk!news.stack.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'subject:code': 0.07; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'checks:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; 'subject:Issue': 0.17; 'jan': 0.18; 'trying': 0.21; 'occurs': 0.22; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; "i'm": 0.29; "skip:' 10": 0.30; 'checks': 0.30; 'to:addr:python- list': 0.33; 'code:': 0.33; 'hi,': 0.33; 'pm,': 0.35; 'received:org': 0.36; 'subject:with': 0.36; 'subject:: ': 0.38; 'easier': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'times': 0.63; 'here': 0.65; 'unnecessary': 0.65; 'counts': 0.81; "'0123456789'": 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Issue with my code Date: Tue, 05 Feb 2013 19:06:03 -0500 References: <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com> 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360109195 news.xs4all.nl 6865 [2001:888:2000:d::a6]:48572 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38248 On 2/5/2013 1:38 PM, maiden129 wrote: > Hi, > > I'm trying to create this program that counts the occurrences > of each digit in a string which the user have to enter. > > Here is my code: > > s=input("Enter a string, eg(4856w23874): ") > s=list(s) Unnecessary conversion. > checkS=['0','1','2','3','4','5','6','7','8','9'] checks = '0123456789' is much easier to type. > > for i in s: > if i in checkS: Loop through checks, not s > t=s.count(i) > if t>1: > for k in range(1,t): > s=s.remove(i) > print(i, "occurs", t,"times.") > > elif t==1: > print(i,"occurs 1 time.") > else: pass Replace everything with s = input("Enter a string of digits: ") for d in '0123456789': c = s.count(d) if c: print("{} occurs {} time{}".format(d, c, '' if c == 1 else 's')) Enter a string of digits: 12233344499 1 occurs 1 time 2 occurs 2 times 3 occurs 3 times 4 occurs 3 times 9 occurs 2 times -- Terry Jan Reedy