Review: Functions


#1

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

Oops, try again. Your shut_down function threw the following error: maximum recursion depth exceeded

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

#2

here:

if shut_down(s):

you call the function, when calling the function inside the function is called recursion

Given your recursion has no base case, you have sort of like an infinity loop but then with functions


#3

Ok.. I somewhat understand.. but what's the solution?


#4

lets look:

# function with parameter s
def shut_down(s):
     print s

# function call with string arguments
print shut_down("yes")
print shut_down("no")

see how the arguments at function call gets passed into the function parameter? So we could compare s with strings to see what the user input to determine what we need to print


#5

I'm lost... new to coding..


#6

the instructions:

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

now you understand what s is, you should be able to understand how to check (and why) s equals "yes" and return the correct string


#8

This is what I have (indentation not pulling through for some reason) - now getting 'yes' is not defined error..

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

shut_down(yes)


#9

here:

shut_down(yes)

why is yes a undefined variable?


#10

That's what I'm trying to figure out...


#11

well, because you haven't defined it anywhere.

looking at the instructions:

Then, if the shut_down function receives an s equal to "yes"

so the argument at function call should be a string, not a (undefined) variable


#12

Ok thanks - I got it to work by entering the string "Shutting down" as the argument. Not really sure why this is correct though.. and to be honest I'm doing these exercises but having a hard time seeing how all this makes sense/fits together - I'm just following instructions without really any context.. I'm hoping at the end of this I can tie everything together and actually get something out of the time I'm spending "learning to code"


#13

well, then else should run (saying sorry), you could also try "no" to see shutdown aborted


#14

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