List + stuck pls


I get the error message File "python", line 8
SyntaxError: 'return' outside function
when I run the code below.
Someone please help

# Write your function below!
def fizz_count(x):
    count = 0
for item in x:
    if item == "fizz":
        count = count +1
    return count


This is the line that calls the function, but your code has it inside the function.


I just switched the code, i still get same error message. Please can you paste the right code to run in clear terms. Forgive my ignorance


After formatting your opening post, we can see that the for loop is not inside the code block.

def fizz_count(x):
    count = 0
for item in x:               # indent 4 spaces
    if item == "fizz":       # indent 4 more spaces
        count = count +1     # indent 4 more spaces
    return count

fizz_count(["buzz","fizz"])  # moved out function body


thank you @mtf for your immediate response. below is the link of the page. I just copied your code and got same error mesage.


Did you correct the indentation as noted?


yes, thank you. It works now.


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