'return' outside function,


#1

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
#2

Remove that line and the code should work as expected.


#3

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.")
else:
speak("I don't know what I'm feeling.")


#4

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
    else:
        # code block

#5

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


#6

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


#7

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