11/15 Problem with code for Count exercise



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

I'm not sure why my code isn't working. I've looked through other posts but I haven't found an answer. Does anyone see something I don't??? Thank you for your time!

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


Given the importance of indentation to mark out the various blocks, the return statement will need extra consideration. Where is it returning from?


Thank you for pointing that out! I shifted the return statement to be in line with the for statement and it worked!


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