 # 11. count: my code should work, tested it

Here’s my code:

``````def count(sequence, item):
count = 0
while item in sequence:
sequence.index(item)
sequence.remove(item)
count = count + 1
return count
``````

When I test various lists and strings, it works. Even when I test the inputs that the program says don’t work, my code returns the correct answers. Any suggestions?

According to your logic this is the code that would work.

``````def count(sequence, item):
count = 0
for i in sequence:
if(item == i):
count = count + 1
return count
``````

but i wanted to know why you wanted to execute these two actions

``````        sequence.index(item)
sequence.remove(item)
``````
1 Like

My logic was to find the item using the index command, then remove the item so it wouldn’t keep counting it. Your code is cleaner, so I’m guessing my index then remove commands are unnecessary. It worked though, so I’m still confused why it didn’t register with codeacademy’s checking program.

Hey Rydan,
I just saw your two line codes on other exercises, could you suggest one here too ?
Also, could you check for any shortening in my code :

``````def count(sequence,item):
return len([i for i in sequence if i == item])``````

on this exercise you can use the python built in `'count'` to make the solution more dynamic. like this

``````def count(sequence, item):
return sequence.count(item)

print(count("knowknowknowknow", "know"))
``````

this will work if your sequence is an array or a string

The only pitfall using this

``````def count(sequence,item):
return len([i for i in sequence if i == item])
``````

is that

``````print(count("knowknowknowknow", "know")) # will be 0 instead of 4

``````

it will only work for lists but you can add this line to make it work for string sequences

``````def count2(sequence,item):
return len([i for i in sequence if i == item]) or len(sequence.split(item))-1
``````

Test it out on labs

5 Likes

The idea was not to use .count() so I went with this more simplistic approach:

``````raw = int(raw_input("Please enter your number sequence now"))
sequence = [raw]
item = int(raw_input("ok, now enter the number to return count on"))

def count(sequence, item):
match = 0
for s in sequence:
if item == s:
match = match + 1
return match``````
1 Like

I guess `for` loop and `if` statement are the best use if we don’t use the .count() function.
I came up with this:

`````` def count(sequence, item):
total = 0
for index, i in enumerate(sequence):
if i == item:
total = total + 1

print count([1,1,1,2,3,4], 1)``````
1 Like

Is there maybe a way to use the sum() for quicker code?
Cheers

Can you explain why my code doesn’t work? yours looks similar but I’m not sure what your use of enumerate() and the index term is doing that makes this work.

def count(sequence, item):
total = 0
for i in sequence:
if i == item:
total = total + 1

boss i don’t understand please what happened to to the found we were asked to creat in the hint.
my code looks like this
def count(sequence, item):
found = 0
for in in sequence:
if sequence[i] == item[i]:
fount+=
return found
please why is it not working

Hi!
Could me explain why this code works, if the item can be a list. I’ve tested and this function doesn’t work with a list as item.

@pymaster30581 Yes, it worked in Code Academy based on the function, not my incorrect use of raw_inputs. I have debugged my actual code in an editor and have fixed by basically making the raw input chars ‘’ iterable through return_list = map(data_type, target_obj_raw_input).
Here is my debugged code with the test code and comments still in it to show the logic and breakpoints I was making through returnable code to screen for inspection of behaviours (ie does it do what I think it does?).

``````raw = raw_input('Please enter your number sequence now: ')
#Make sure "raw" has a car quote '' so a string literal is not returned

item = int(raw_input("ok, now enter the number to return count on: "))
# on "item" its ok to cast a string "" to int as its not iterable

sequence = map(int, raw) #use the map function to return the numbers as ints
print "Raw obj total list return as mapped ints is ", sequence # Test Code helps LOADS in debugging to checkpoint behaviours

def count(sequence, item):
match = 0
for s in sequence:
if item == s:
print "match hit for var s is ", s #Test Code
match = match + 1
return match
print count(sequence, item)
print "Total Word Count Detections is ", count(sequence, item) #Test Code``````

Hey,

The code you have should work by the look of it, maybe there was a fault in syntax indentation? was your code laid out the following way:

``````def count(sequence, item):
total = 0
for i in sequence:
if i == item:
total = total + 1
The `index` and `enumerate` in the `for` loop is a different form of `for` loop which pretty much does the same thing for this task.