17. Review: Functions


#1


So I resumed my python courses because I got stuck on functions and lists the last time.
Repeating chapter 4 at the moment and I can't seem to figure out what I am doing wrong. The code is giving me a return outside of function error.

I know I can just do

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

But what if I want to do it like below?


def shut_down(s):
    return s
    
if shut_down(yes):
    return "Shutting Down"
elif shut_down(no):
    return "Shutdown Aborted"
else:
    return "Sorry"

Why is the above code returning a return outside of function error?


#2

Return is only used inside a function (defined with def). So line 1 and 2 will work, but in if/else/elif statements, use print.

You are also declaring your if statements like a function.

def shut_down(s):
    return s
    
if shut_down == "yes":
    print "Shutting Down"
elif shut_down == "no":
    print "Shutdown Aborted"
else:
    print "Sorry"

This code works. :slight_smile:


#3

seems you been out it for a while. Lets break it down, if we have a function with parameter s:

def example(s):
   print s

s will hold the value you pass as argument into the function call, see:

def example(s):
   print s
example("this is an argument")

so with this knowledge we could compare s with "yes":

def example(s):
   if s == "yes":
     return "Shutting Down"
print example("yes")

notice how the indention of if is, due to the indention if is now nested inside your function.

one more thing, a function ends the moment a return keyword is reached. the function hands you something back. we can display what is returned to the out[put window by calling print on the function call.


#4

@simongh, your code is wrong, and it doesn't work, there are some mistakes in it


#5

Thanks for the responses.

I'm still stuck.
The exercise tells to not use print but the return output;

                Ensure your function outputs appear exactly as shown!
                Also, ensure your function returns the above values rather than printing them.

Why am i first returning "Shutting Down" and then printing example("yes") ?


#6

can you post an updated version of your code?

if you just place this:

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

inside a function it should work fine:

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

right?

the other code you use, won't work. given return an only exist inside a function.

if course you can do something like:

user = raw_input("would you like to shut the computer down?")
if user == "yes":
   print "shutting down"

but this will not let you pass the exercise


#7

Hmm If I try pasting in the function it works, thanks for that.

But I still don't understand how the current code doesn't work;

def shut_down(s):
   return s
if shut_down == "yes":
    print "Shutting Down"
elif shut_down == "no":
    print "Shutdown Aborted"
else:
    print "sorry"

it returns as an error:

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


#8

well the exercise is going to call your function (just your function) to verify it works correctly, and it expects certain strings to be returned based on the argument the exercise passes into the function call, that is currently not happening


#9

I see....

Thanks for the responses.


#10

so you understand now? just out of curiosity, what nationality are you? (if you don't feel like answering that, don't), koffiebeker is a dutch word, or is that just random?


#11

No I am dutch indeed. I am addicted to coffee, thats why I choose it a while ago and it kind of turned into my online handle. :wink:


#12

koffieverslaafd :stuck_out_tongue: good to meet another dutchie here on the forum


#13

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