Count - Fails on Foo


Hi all

I'm stuck on count.

My code looks similar to other solutions on the forums but it returns the error below and i can't figure out why:

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

def count(sequence, item):
    found = 0 #initalise found at 0
    for i in sequence: #loop through sequence
        if i == item: #if i is the same as item
            found += 1 #increment found by one
        return found

Any suggestions greatly appreciated!



a function ends the moment a return keyword is reached, if the return keyword is reached in a loop, the loop breaks, the function ends


Thanks, got it to work by converting item to a string.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.