17. Review: Functions (Python)


#1
  1. Review: Functions

This is the code that I am inputting:

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

I am receiving this error message:
Oops, try again. Did you create a function called shut_down?

Can anyone help me figure out what I am doing wrong? I very much appreciate your help.


#2

Remember that a string is always surrounded with quote marks, so change yes and no to "yes" and "no".


#3

Thank you, @toplearner. I have made edits and added quotes to the strings. Thank you for this reminder. However, I am now getting the error message, "Oops, try again. Your shut_down function threw the following error: maximum recursion depth exceeded in cmp"


#4

can we see an updated version of your code? Going by the error, you added a function call inside the function, but we know nothing until you post your code


#5

Thank you so much for following up. Here is my code:

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

#6

Actually, I changed my code to this:

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

And it worked. I'm not sure what I was thinking with what I last submitted. I worked on it about a week ago, so I will try to remember what my thought process was. I appreciate your help. I'm glad it was something small.


#7

I did not have "return" in this block of code, so I think that I didn't really provide enough instruction for it to complete the function. Sorry if that sounds dumb or unclear. I'm very new LOL

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

#8

here:

shut_down("Shutting down")

you call the function inside the function, this is recursion. But given there is no way for the recursion to end, you have something similair to an infinity loop

you did find the right solution:

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

if we then call the function:

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

print shut_down("yes")

we can see the function return the correct string


#9

Perfect. I really appreciate your explanation. Thank you for your time!


#10

#11

you're welcome, good i could clarify something