Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Issue with my code (http://www.velocityreviews.com/forums/t957309-issue-with-my-code.html)

maiden129 02-05-2013 06:38 PM

Issue with my code
 
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'

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


maiden129 02-05-2013 06:43 PM

Re: Issue with my code
 

Also I’m using Python 3.2.3.

On Tuesday, February 5, 2013 1:38:55 PM UTC-5, 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'
>
>
>
> 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



marduk 02-05-2013 06:56 PM

Re: Issue with my code
 


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.

MRAB 02-05-2013 07:06 PM

Re: Issue with my code
 
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! :-)

maiden129 02-05-2013 07:20 PM

Re: Issue with my code
 
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?

maiden129 02-05-2013 07:20 PM

Re: Issue with my code
 
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?

Dave Angel 02-05-2013 07:43 PM

Re: Issue with my code
 
On 02/05/2013 02:20 PM, maiden129 wrote:
> On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:
>> On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote:
>>

<Snipping double-spaced googlegroups trash>
>
> 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?
>


As MRAB pointed out, don't delete items from a list you're iterating
over. It can make the iterator go nuts. He suggests the collections
module.

But if you want to do it by hand, one approach is to reverse the two
loops. Iterate over the characters in CheckS list, examining the entire
s list for each one and figuring out how many times the character occurs.

Another approach is to build a dict, or a defaultdict, to keep counts
for each of the characters in CheckS.

--
DaveA

maiden129 02-05-2013 08:19 PM

Re: Issue with my code
 
How to reverse the two loops?

On Tuesday, February 5, 2013 2:43:47 PM UTC-5, Dave Angel wrote:
> On 02/05/2013 02:20 PM, maiden129 wrote:
>
> > On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:

>
> >> On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote:

>
> >>

>
> <Snipping double-spaced googlegroups trash>
>
> >

>
> > 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?

>
> >

>
>
>
> As MRAB pointed out, don't delete items from a list you're iterating
>
> over. It can make the iterator go nuts. He suggests the collections
>
> module.
>
>
>
> But if you want to do it by hand, one approach is to reverse the two
>
> loops. Iterate over the characters in CheckS list, examining the entire
>
> s list for each one and figuring out how many times the character occurs.
>
>
>
> Another approach is to build a dict, or a defaultdict, to keep counts
>
> for each of the characters in CheckS.
>
>
>
> --
>
> DaveA



maiden129 02-05-2013 08:19 PM

Re: Issue with my code
 
How to reverse the two loops?

On Tuesday, February 5, 2013 2:43:47 PM UTC-5, Dave Angel wrote:
> On 02/05/2013 02:20 PM, maiden129 wrote:
>
> > On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:

>
> >> On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote:

>
> >>

>
> <Snipping double-spaced googlegroups trash>
>
> >

>
> > 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?

>
> >

>
>
>
> As MRAB pointed out, don't delete items from a list you're iterating
>
> over. It can make the iterator go nuts. He suggests the collections
>
> module.
>
>
>
> But if you want to do it by hand, one approach is to reverse the two
>
> loops. Iterate over the characters in CheckS list, examining the entire
>
> s list for each one and figuring out how many times the character occurs.
>
>
>
> Another approach is to build a dict, or a defaultdict, to keep counts
>
> for each of the characters in CheckS.
>
>
>
> --
>
> DaveA



darnold 02-05-2013 09:37 PM

Re: Issue with my code
 
On Feb 5, 2:19*pm, maiden129 <sengokubasarafe...@gmail.com> wrote:
> How to reverse the two loops?
>


s=input("Enter a string, eg(4856w23874): ")

checkS=['0','1','2','3','4','5','6','7','8','9']

for digit in checkS:
t = s.count(digit)
if t == 0:
pass
elif t == 1:
print(digit,"occurs 1 time.")
else:
print(digit, "occurs", t,"times.")


>>>

Enter a string, eg(4856w23874): 23493049weee2039412367
0 occurs 2 times.
1 occurs 1 time.
2 occurs 3 times.
3 occurs 4 times.
4 occurs 3 times.
6 occurs 1 time.
7 occurs 1 time.
9 occurs 3 times.
>>>


HTH,
Don


All times are GMT. The time now is 03:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.