Issue with my code

M

maiden129

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
 
M

marduk

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

Also, there are various inefficiencies in your code, but that is the
main issue with the AttributeError.
 
M

MRAB

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! :)
 
M

maiden129

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:

if t>1:
for k in range(1,t):

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'



s=s.remove(i) does not return a new list but modifies the list in

place.



So you probably just want





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

maiden129

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:

if t>1:
for k in range(1,t):

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'



s=s.remove(i) does not return a new list but modifies the list in

place.



So you probably just want





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

Dave Angel

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.
 
M

maiden129

How to reverse the two loops?

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.
 
M

maiden129

How to reverse the two loops?

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.
 
D

darnold

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
 
M

marduk

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.

Although that implementation also scans the string 10 times (s.count()),
which may not be as efficient (although it is happening in C, so perhaps
not).

A better solution involves only scanning the string once.
 
D

Dennis Lee Bieber

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)

Let's see... for each character in the input, see if that character
is in the check list, and if it is, then reverse and count the
occurences in the input string. (Oh, that's after you converted the
input to a list of single characters)

if t>1:
for k in range(1,t):
s=s.remove(i)
print(i, "occurs", t,"times.")


Oh Oh.... Besides the fact that .remove() does its work in-place
(and doesn't return a value -- hence the "s=" is binding None to s), you
are trying to modify the string that you are looping over... And that is
a no-no -- it will cause you to skip over items.

-=-=-=-=-=-.... if c == "3":
.... s.remove(c)
.... print "c: %s\tREMOVED" % (c,)
.... else:
.... print "c: %s\ts: %s" % (c, s)
....
c: 1 s: ['1', '2', '3', '4', '5', '6', '7', '8', '9']
c: 2 s: ['1', '2', '3', '4', '5', '6', '7', '8', '9']
c: 3 REMOVED
c: 5 s: ['1', '2', '4', '5', '6', '7', '8', '9']
c: 6 s: ['1', '2', '4', '5', '6', '7', '8', '9']
c: 7 s: ['1', '2', '4', '5', '6', '7', '8', '9']
c: 8 s: ['1', '2', '4', '5', '6', '7', '8', '9']
c: 9 s: ['1', '2', '4', '5', '6', '7', '8', '9']-=-=-=-=-=-

Note how there is no line for c=4. When you remove the "3", all the
rest shift left to fill in -- but the next round of the loop is going to
"increment" to the character after the position that "3" was at... That
is now the "5".

Rather than working from the input list, why not turn it around...

-=-=-=-=-=-.... o = inpt.count(d)
.... if o:
.... print "%s occurs %s times" % (d, o)
....
0 occurs 1 times
2 occurs 1 times
3 occurs 1 times
4 occurs 3 times
5 occurs 1 times
6 occurs 1 times
7 occurs 1 times
8 occurs 2 times-=-=-=-=-=-
 
D

darnold

Although that implementation also scans the string 10 times (s.count()),
which may not be as efficient (although it is happening in C, so perhaps
not).

A better solution involves only scanning the string once.

agreed. i was specifically showing how to reverse the loop.
using the much-better-suited Counter class:


from collections import Counter

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

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

cnt = Counter()

for char in s:
cnt[char] += 1

for char, tally in sorted(cnt.items()):
if char in checkS and tally > 0:
if tally == 1:
print(char,"occurs 1 time.")
else:
print(char, "occurs", tally,"times.")
Enter a string, eg(4856w23874): 192398209asdfbc12903348955
0 occurs 2 times.
1 occurs 2 times.
2 occurs 3 times.
3 occurs 3 times.
4 occurs 1 time.
5 occurs 2 times.
8 occurs 2 times.
9 occurs 5 times.
HTH,
Don
 
T

Terry Reedy

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)

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

checks = '0123456789' is much easier to type.
for i in s:
if i in checkS:

Loop through checks, not s
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

Replace everything with

s = input("Enter a string of digits: ")
for d in '0123456789':
c = s.count(d)
if c:
print("{} occurs {} time{}".format(d, c, '' if c == 1 else 's'))

Enter a string of digits: 12233344499
1 occurs 1 time
2 occurs 2 times
3 occurs 3 times
4 occurs 3 times
9 occurs 2 times
 
R

rusi

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

Pythons 2.7 and later have dictionary comprehensions. So you can do
this:

{'a': 1, 'b': 1, '1': 2, '3': 1, '2': 2, '4': 1}

Which gives counts for all letters. To filter out the digit-counts
only:
{'1': 2, '3': 1, '2': 2, '4': 1}

You can then print out the values in d in any which way you want.
[Starting with printing is usually a bad idea]
 
R

rusi

Pythons 2.7 and later have dictionary comprehensions. So you can do
this:

{'a': 1, 'b': 1, '1': 2, '3': 1, '2': 2, '4': 1}

Which gives counts for all letters. To filter out the digit-counts
only:

{'1': 2, '3': 1, '2': 2, '4': 1}

You can then print out the values in d in any which way you want.
[Starting with printing is usually a bad idea]

Sorry cut-paste slip-up.
1. use dig or digits (or none, just inline the "0123456789")
2. I assumed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top