'return' outside function,


I keep getting File "python", line 5
SyntaxError: 'return' outside function, but my returns are indented. I don't think the instructions are very clear on most of these examples.

def shut_down(s):
return s

if s == "yes":
return "Shutting Down"

elif s == "no":
return "Shutdown Aborted"

elif s != "yes" or "no":
return "Sorry"

(the indents are not showing in this post but are in my code)

Review: Functions

Remove that line and the code should work as expected.


Okay, that worked but I still had to add more indents. It wasn't clear to me that you have to add another indent every time you define something new. Also it is confusing that the example in this case shows a "return message" right after the definition of the function. I thought that was what I was supposed to do since the example is structured that way:

def speak(message):
return message

if happy():
speak("I'm happy!")
elif sad():
speak("I'm sad.")
speak("I don't know what I'm feeling.")


Just as above, the return line should be at the end of the function, not the beginning (unless it is conditional).

Header lines do not get indentation, so,

def func(param):

The code inside the function is a block, so all lines inside the function have at least one indent (four spaces recommended)

def func(param):
    # code indented four spaces

for, if and while are constructs that have code blocks, so their block is indented.

    if condition:
        # code block indented
    elif other_condition:
        # code block
        # code block


Thanks, so is the example just wrong? I've found a couple others that will generate errors as written.


Can you post a link to the lesson where this example is given, please?


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