"Lists + Functions" - What am I doing wrong?


#1

I cannot see how the way I did it is incorrect.

Instructions:

  1. Write a function that counts how many times the string “fizz” appears in a list.
    -Write a function called fizz_count that takes a list x as input.
    -Create a variable count to hold the ongoing count. Initialize it to zero.
    -for each item in x:, if that item is equal to the string “fizz” then increment the count variable.
    -After the loop, please return the count variable.
    For example, fizz_count([“fizz”,“cat”,“fizz”]) should return 2.

x = [“fizz”,“shizz”,“dizz”,“fizz”,“biz”,“fizz”]
def fizz_count(x):
[indent]count = 0
[indent]for variable in x:
[indent][indent]if variable == “fizz”:
[indent][indent][indent]count = count + 1
return count

The error I’m getting comes from “return count” - SyntaxError: ‘return’ outside function
It suggests to me it doesn’t know what I’m trying to return. It thinks “count” is some random function not associated with anything in the code I wrote… but it is right there, in the function I defined.

This cryptic bit at the end of the instructions that says “For example, fizz_count([“fizz”,“cat”,“fizz”]) should return 2.” is probably relevant. I’m confused about why their list is in parentheses and brackets… Also, I just defined a function called “fizz_count” I can’t then also define a variable called fizz_count… and even if I did, that wouldn’t suddenly make my code correct would it? I could call it “n” and the code would be equally incorrect.


#2

To be inside the function, that line needs also to be indented like those above it.


#3

Oh god, that’s so simple. Thank you.


#4

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