This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Trisha Khattar
about 10 years

SyntaxError: 'return' outside function

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

My indenting is fine, I think, even though the indenting doesn’t show here. The error is: File “python”, line 7 SyntaxError: ‘return’ outside function

Answer 53bf2a587c82caf33a0003a4

2 votes

Permalink

After you copy and paste your code into your post, you need to format it so we can see indentation, underscores, and other details. For instructions on formatting, see the link that looks like this:

Show formatting instructions ▼

Are you sure your indentation is correct? We cannot check it, because your code is not formatted, but your return statement needs to be indented by exactly one level, in order for it to be properly situated in the fizz_count function, without its being part of the for loop or the if block.

Here is the same code that you posted, but with correct indentation visible, so you can compare it to what you submitted to Codecademy’s Python interpreter:

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

Each portion of the code is indented by one level to the right, with respect to its controlling header.

points
Submitted by Glenn Richard
about 10 years

1 comments

Trisha Khattar about 10 years

THANK YOU SO MUCH!!! I appreciate it. :)