Lists + Functions


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/0/4?curriculum_id=4f89dab3d788890003000096#


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


#2

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 for..in statement ends.


#3

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


#4

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