Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'example:': 0.03; 'elif': 0.04; 'attribute': 0.05; 'error:': 0.05; 'none,': 0.05; 'subject:code': 0.07; 'collections': 0.09; 'none.': 0.09; 'occurrences': 0.09; ':-)': 0.13; 'checks:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'string:': 0.16; 'string': 0.17; 'wrote:': 0.17; "shouldn't": 0.17; 'string,': 0.17; 'subject:Issue': 0.17; 'changes': 0.20; 'trying': 0.21; 'occurs': 0.22; 'this:': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'executing': 0.27; 'module.': 0.27; 'class': 0.29; "i'm": 0.29; 'to:addr:python-list': 0.33; 'code:': 0.33; 'hi,': 0.33; 'list': 0.35; 'add': 0.36; 'but': 0.36; 'wanted': 0.36; 'method': 0.36; 'subject:with': 0.36; 'itself': 0.37; 'keeps': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'list,': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'most': 0.61; 'remove': 0.61; 'first': 0.61; 'time,': 0.62; 'show': 0.63; 'here': 0.65; 'header:Reply-To:1': 0.68; 'reply- to:no real name:2**0': 0.72; 'counts': 0.81; 'case?': 0.84; 'reply-to:addr:python.org': 0.84; "'remove'": 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=XeZXOvF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=8vuQrxt87CgA:10 a=0PABKfTFvorNYfjudEkA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Tue, 05 Feb 2013 19:06:46 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Issue with my code References: <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com> In-Reply-To: <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 1360091206 news.xs4all.nl 6989 [2001:888:2000:d::a6]:56022 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38217 On 2013-02-05 18:38, 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) > > checkS=['0','1','2','3','4','5','6','7','8','9'] > > for i in s: > if i in checkS: > t=s.count(i) > if t>1: > for k in range(1,t): > s=s.remove(i) The 'remove' method changes the list itself and then returns None, so after executing this line the first time, s will be None. > print(i, "occurs", t,"times.") > > elif t==1: > print(i,"occurs 1 time.") > else: pass > > but it keeps showing this error: > > t=s.count(i) > AttributeError: 'NoneType' object has no attribute 'count' > > I wanted to show like this: > > Example: > > Enter a string: 3233456 > > 3 occurs 3 > 2 occurs 1 > 4 occurs 1 > 5 occurs 1 > 6 occurs 1 > You shouldn't add or remove items from a collection, such as a list, over which you're iterating. Is it even necessary in this case? No. Have a look at the Counter class in the collections module. That'll let you eliminate most of your code! :-)