17.Review Functions


#1

My code runs and there is no error after running in the interpreter but it does not print the required output

def shut_down(s):

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

I need help
#2

maybe try moving the print above return...


#3

That's correct. It never arrived at the command to print because the return statement ends and exits the function.


#4

Can you explain why this is so? I am having the same issues with this code.


#5

Sure. Consider the code that was posted earlier:

if( s == "yes"):
    return "Shutting down"
    print "Shutting down"

If s is "yes", we advance to the next line. That line says to return the string "Shutting down". After a return statement, the function stops executing. So we never have a chance to get to the print command on the next line. This is also true for the print commands in the elif and else conditionals.


#6

So how do I fix this because I keep getting an error message.


#7

Remove the print statements and see what happens. If that doesn't work, please tell us what the error message says.


#8

Got it! That fixed it! Thank you so much!

correct code:
def shut_down(s):
if( s == "yes"):
return "Shutting down"

elif(s == "no"): 
    return "Shutdown aborted"

else:
    return "Sorry"

#9

Thanks a Lot. Helpful :slightly_smiling:


#10

why there is bracket (s =="yes")