17.review functions NEED HELP


#1



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


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

Your code threw the following error: 'return' outside function (python, line 5)


not sure what is outside function?? what's wrong with my code?


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


#2

You mixed up things here,
To define a function we use this skeleton:

def function_name(optional_parameter):
    #dosomecode
    return things

Things to know about return:
return keyword simply is an exit door for function, when function hits return statement, It simply exit outs of it.
so delete early introduction of this line.
return s

Now this is how we define if/elif/else statements.

if test:
    #Code
elif test:
    #Code 
elif test:
    #Code  
else:
    #Code

things to note:
if/elif/else always starts with if, number of elif's can be infinite (theoretically speaking), the else block is optional.
if / elif's must have some boolean expressions (test), else goes without any expression.

The conditional expressions like this s == 'yes' should be in place of test that I written in skeleton.

also as if/elif/else are inside the function, Indent them with 4 spaces.

Like this skeleton..

def someFunction(some parameter):
    if test:
        #Code
    elif test:
       #Code 
    elif test:
        #Code  
    else:
        #code

Now start again,read theory,examples ,instructions again!


#3

Thank you! This is super helpful to me :slightly_smiling_face:


#4

Thank you for your awesome explanation!


#7

This still doesn't work for me!

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

what is the mistake I have done?


#8

You should be seeing this in the console window:

File "python", line 4
if s == 'yes':
^
IndentationError: expected an indented block

If yes then see @greentreemee 's post above. Your if, elif and else statments need to be indented.


#9

yes.I saw the post.Got the same thing, now corrected it! So thanks! @jagking


#11

I followed your instruction and that worked, but actually did not print anything on the console window!
How can I fix this?

def shut_down(s):

if s == "yes":
return "Shutting down"
elif s == "no":
return "Shutdown aborted"
else:
return "Sorry"

shut_down(yes)


#12

shut_down(yes)

If you want something to appear in the console window use print.

Also, yes should be a string.


#13

@greentreemee Awesome explanation. Thanks man!!!


#14

def shut_down(s):

if 'yes':
    return "Shutting down"
elif 'no':
    return "Shutdown aborted"
else:
    return "sorry"

what am i doing wrong?


#15

In your if/elifstatements, instead of just typing the string, you need to check if the argument is equal (==) to the string provided in your statements.

Review your instructions. Here's the second instruction (#2):

Then, if the shut_down function receives an s equal to "yes", it should return "Shutting down"

Also, make sure your code is indented under your function.


#16

What's wrong with my code?
it keeps saying
Traceback (most recent call last):
File "python/cli_runner.py", line 152, in

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


#17

Maybe indentation is the problem, try to format so we can see what's the problem.
(why colon that ends if's header looks different from other colons ? )
Note:
To learn more about formatting a code.


#18

Thanks.
I unconsciously used the wrong colon. And now the problem is solved.


#19

def shut_down(s):

if s == "yes":
    return "Shutting down"
elif s == "no":
    return "Shutdown aborted"
else:
    return "Sorry"

shut_down(yes)

Any idea why this doesnt work? Your code looks a bit off. Feel free to peek back at earlier exercises if you need a refresher! Your code threw the following error: expected an indented block (python, line 3)


#20

You indentation is off. Everything except the shut_down(yes) should be included within the function (as in indented so that it is not on the same level as the function).


#21

Thank you very much. :smiley:


#22

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

shut_down("yes")

what am I doing wrong here?

It says "Oops, try again. Your function failed on the message yes. It returned 'yes' when it should have returned 'Shutting down'".


#23

Look at your first line of the funtion:

def shut_down(s):
    return s

You are returning the input before anything else can happen.