Lists + Functions


I solved this problem but I don't understand why my return count isn't tabbed with "if item == "'fizz' " it's instead tabbed with "for item in x"

I would expect the return count to be tabbed with the "if item == 'fizz' "

def fizz_count(x):
    count = 0
    for item in x:
        if item == "fizz":
            count += 1
    return count


If your return is part of your if block, then the first time you hit a "fizz" the return statement will run which means you will exit the function.

If you think about the logic here, you don't want to return until you have counted ALL the "fizz" instances, which means after the statement ends.


It is important that the iteration of list x is completed before returning a count, else it will not be full count.


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