Issue with my code
python at mrabarnett.plus.com
Tue Feb 5 20:06:46 CET 2013
On 2013-02-05 18:38, maiden129 wrote:
> 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): ")
> for i in s:
> if i in checkS:
> if t>1:
> for k in range(1,t):
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:
> AttributeError: 'NoneType' object has no attribute 'count'
> I wanted to show like this:
> 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! :-)
More information about the Python-list