SyntaxError: 'Return' Outside Function


#1



Hey there, mates. I'm having issues in the Review: Functions. It's been maybe four days (big break) since I've coded a bit, but I know I remember almost all I've been taught.

https://www.codecademy.com/en/courses/python-beginner-c7VZg/4/1?curriculum_id=4f89dab3d788890003000096


However, I get this message: File "python", line 4
SyntaxError: 'return' outside function


I expected the code to work correctly as I had followed what I know, but I am incorrect. But on what?


Replace this line with your code. 
def shut_down(s):
    return s
if s == "yes":
    return "Shutting down"
elif s == "no":
    return "Shutdown aborted"
else:
    return "Sorry"


#2
  1. The return keyword immediately exits a function -- you have return s as the second line of your function -- that means your function ends there - is that what you intended?

  2. Also, because of the way that you have indented your lines, your if statement is not part of your shut_down function. If you want the if statement to be part of the function you are going to have to indent it.


#3

Thank you for responding! I think that that was intended as it is shown in the example picture that you'll have "return message" on the second line. To respond to the second part of that, would it look something like this?

def shut_down(s):
    return s
    if s == "yes":
        return "Shutting down"
    elif s == "no":
        return "Shutdown aborted"
    else:
        return "Sorry"

Turns out that does not work. How would I go about indenting the code? It now says s returns as "yes" instead of "Shutting down."

`


#4

If you leave the return statement on the second line then the function will exit on that line every time.

Your indenting is now correct.

It is returning "yes" because of line 2. When the code checker checks your function by sending in "yes", it exits on line 2.


#5

Oh my God! Thank you so much! It makes loads of sense to me now. I've replaced the "return s" on the second line with "print s," and it works. Thank you!


#6

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