Count. "Fails on foo"


#1



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


When running my code for the lesson I get this error message:
"Your function fails on count([4, 'foo', 5, 'foo'],5). It returns 0 when it should return 1."


I'm not sure why this happens. it shouldn't fail because I am converting it to a string so it should be able to accept numbers and words.


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


#2

Check your indentation of your return statement. So far, you're only looking at the first item and stops since your return is indented below the for loop.

I've used another test to figure this out

print count([1,2,1,1], 1)
>> 1 instead of 3

#3

thanks. cant believe i spent so long being angry at a simple indentation error.


#4

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