List + Function : Help

I dont understand why I am getting this error: “Oops, try again. fizz_count([‘fIzZ’, 2, ‘buzz’, 1, ‘fIzZ’, 7]) returned None instead of the correct answer: 0”

Here is the code:

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

        
x = ["fizz","cat","fizz"]
get_fizz_count = fizz_count(x)
print get_fizz_count

two things:

a function ends the moment a return keyword is reached.
currently, if a match is found, count is increased by one and count gets returned, causing the function to end. your return keyword is only reached when fizz is found, otherwise no return keyword is found, so the default (None) gets returned

you want to find all instance of fizz in your list, then at the end, return count. Place return count outside the loop

2 Likes

You need to return count in the main body of your function, not as a part of the if statement or the for loop. Move the indentation back so it is inline with the for loop.

1 Like

Thank you. I just solved it.