10. A Day at the Supermarket - 4.Lists + Functions



<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
fizz_count([‘fIzZ’, 7, ‘fizz’, ‘fIzZ’, 1, 6]) returned 0 instead of the correct answer: 1

<What do you expect to happen instead?>
Not sure why I am getting an error message listing list items which do NOT feature in my list!


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

ello = [‘fiz’,‘fizz’,‘fizz’]
howmanytimes = fizz_count(ello)
print howmanytimes

<do not remove the three backticks above>


Check your indentation. Remember, Python runs the code by how it’s indented. For example, a conditional expression like if runs the code that is indented beneath it when the conditional expression is true.

        # IF item is equal to 'fizz', then the indented
        # codeblock runs. Else, it skips over it.
        if item == 'fizz':
            # everything that is indented here is part of
            # `if` codeblock.  It only runs IF item == 'fizz'.
            count = count + 1

Check your code for proper indentation where it’s needed to define the code blocks for things like def, for, and if.


Thanks a lot for responding promptly.
I just checked the forums and seems like a lot of people have been stumbling on this very exercise due to the strange error messages it spits out.
In any case, I re-wrote my code and it is working now. I removed the last 3 lines of my code and replaced it with a print statement. :neutral_face:


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