Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Issue with my code

Reply
Thread Tools

Issue with my code

 
 
maiden129
Guest
Posts: n/a
 
      02-05-2013
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

 
Reply With Quote
 
 
 
 
maiden129
Guest
Posts: n/a
 
      02-05-2013

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


 
Reply With Quote
 
 
 
 
marduk
Guest
Posts: n/a
 
      02-05-2013


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.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      02-05-2013
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!
 
Reply With Quote
 
maiden129
Guest
Posts: n/a
 
      02-05-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?
 
Reply With Quote
 
maiden129
Guest
Posts: n/a
 
      02-05-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?
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      02-05-2013
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
 
Reply With Quote
 
maiden129
Guest
Posts: n/a
 
      02-05-2013
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


 
Reply With Quote
 
maiden129
Guest
Posts: n/a
 
      02-05-2013
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


 
Reply With Quote
 
darnold
Guest
Posts: n/a
 
      02-05-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Service Pack 2: login issue's and power management issue's ?!? Skybuck Flying Windows 64bit 0 04-07-2007 03:12 PM
inspiron 8200 video issue and hd issue the pez lover Computer Support 1 02-05-2007 02:44 AM
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Major ActiveX Domain issue. NOT LOCAL PC ISSUE joe.valentine@gmail.com Computer Support 8 02-06-2006 09:03 PM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57