Issue with my code

maiden129 sengokubasarafever at gmail.com
Tue Feb 5 20:20:19 CET 2013


On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:
> On Tue, Feb 5, 2013, at 01: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)
> 
> > 
> 
> > 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)
> 
> >                 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'
> 
> 
> 
> s=s.remove(i) does not return a new list but modifies the list  in
> 
> place.
> 
> 
> 
> So you probably just want
> 
> 
> 
> >>> s.remove(i)
> 
> 
> 
> Also, there are various inefficiencies in your code, but that is the
> 
> main issue with the AttributeError.

when I removed "s.remove(i), it starts to repeat the number of occurrences too 

many times like this:

2 occurs 3 times.
2 occurs 3 times.
3 occurs 3 times.
3 occurs 3 times.
2 occurs 3 times.
2 occurs 3 times.
5 occurs 1 time.
3 occurs 3 times.
3 occurs 3 times.
4 occurs 1 time.
3 occurs 3 times.
3 occurs 3 times.
1 occurs 1 time.
2 occurs 3 times.
2 occurs 3 times. 

How can I stop this?



More information about the Python-list mailing list