11/15 Count Test Error?


#1

My code

def count(sequence,item):
    res = 0
    while item in sequence:
        sequence.remove(item)
        res +=1
    return res
    
print(count([4, 'foo', 'foo'],5))

Error: Oops, try again.
Your function fails on count([4, 'foo', 'foo'],5). It returns 1 when it should return 0.

Output:
0
None


#2

HI this line

while item in sequence:

I think that where you put while you should put for


#3

No.
Example:
4 in [5,6,7,8] #=> False
4 in [5,6,7,4,2,6] #=> True


#4

Hi, what was the solution for this? I am stuck at a similar situation but do not know on how to proceed with this further.


#5
def count(sequence,item):
    return sequence.count(item)

#6

this solution worked for me-

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

#7

Your Code seems to be giving wrong o/p. Still don't know how is it accepting the solution.


#8

It looks like it is giving to correct output to me- there is only one key in the sequence that is an exact match to 2. I could be wrong, but I believe that it does not count the 2 in your list at index 1 because [2,1] is not an exact match to your it variable.