Count - Fails on Foo


#1

Hi all

I'm stuck on count.

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/1?curriculum_id=4f89dab3d788890003000096

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!

Thanks


#2

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


#3

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


#4

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