11/15 Problem with code for Count exercise


#1



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


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


#2

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


#3

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


#4

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