4. Lists+functions - code doesnt work


#1



Hello,
I'm stuck at exercise 4, in day at the supermarket(https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/0/4?curriculum_id=4f89dab3d788890003000096)


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

it seems that my code is wrong because it only does count = count +1 if the first item on the list =="fizz"
i really don't know whats wrong, there is no topic with the same problem and i read my code like 15 times over and over but i'm absolutely sure it is right.

the error message say: Oops, try again. fizz_count(['buzz', 8, 'fuzz', 'fuzz', 'fizz', 'fizz']) returned 0 instead of the correct answer: 2

If the list begins with fizz it returns 1
can anyone figure out the mistake.


#2

a function ends the moment a return keyword is reached, if a return keyword is reached in the loop, the loop will break

so your loop breaks in its fist iteration


#3

thanks, but how do i fix it then?
in the explanation there's an example code and mine is exactly similar but it doesnt work


#4

place return outside the loop so the whole loop can run and doesn't break in its first iteration


#5

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