List + Functions Syntax Error on return count


#1

Hi, the interpreter is giving me a syntax error on my code, shown below:

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

return count

If someone could point out my error that would be great. Thanks.


#2

Sorry for not showing the error. Here's the error code:

  File "python", line 9
SyntaxError: 'return' outside function

#3

SyntaxError: 'return' outside function

That explains it.

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

    return count

#4

Return simply needs to be indented in align with for?


#5

@omarbawab6

No, it needs to be indented because in python anything that does not match a function/class in indentaion will not be included.

Example

#Notice how everything is indented the same
#You still have to follw indent rules for statements.
def function_junction(whats): 
    your = "malfunction?"
    return whats + your

Now if we don't properly indent stuff will not be included. It is called scope in python.


#6

Got it, big thanks for the help!