11/15 Count Test Error?


My code

def count(sequence,item):
    res = 0
    while item in sequence:
        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.



HI this line

while item in sequence:

I think that where you put while you should put for


4 in [5,6,7,8] #=> False
4 in [5,6,7,4,2,6] #=> True


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.

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


this solution worked for me-

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


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


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.