17 Function Review Getting S Defined


#1

Just wondering how I need to define s in this situation so I can make s equal to yes or no. The error message I keep getting is NameError: name 's' is not defined


def shut_down(s):
    return s


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


#2

That will be done when yes or no are passed as arguments in a function call...

shut_down("yes")

shut_down("no")

The return statement should not be the first line in the function. We need to return a string expression based upon the parameter...

def func(parameter):
    if parameter == 'yes':
        return "..."
    elif parameter == "no":
        return "..."
    else:
        return "..."

#3

So I have set up my code like that by getting rid of return s and moving everything into the correct spacing but now I get Your shut_down function threw the following error: maximum recursion depth exceeded in cmp and I have no idea what that means...

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

#4

Should be,

return "Shutting down"

LIkewise the other two branches.


#5

Thank you very much! However, in the example given in that lesson it shows as follows obviously:

def speak(message):
    return message

if happy():
    speak("I'm happy!")
elif sad():
    speak("I'm sad.")
else:
    speak("I don't know what I'm feeling.")

I was just curious since speak was used in the example why not shut_down? Or is that just with the example or does that apply to another situation? Or am I just confusing myself? xD


#6

Again, the example code above is just there for your reference!

The above example is not very well explained, and has mislead many learners over the same confusion as you are experiencing. Notice that happy() and sad() are both function calls. We can only assume that they both return True or False.

However, if we follow the instructions without regard for the example, the code falls into place...

Instructions
First, def a function, shut_down, that takes one argument s. Don't forget the parentheses or the colon!
Then, if the shut_down function receives an s equal to "yes", it should return "Shutting down"
Alternatively, elif s is equal to "no", then the function should return "Shutdown aborted".
Finally, if shut_down gets anything other than those inputs, the function should return "Sorry"


#7

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