Review Functions: "Plugging In" for Functions


When I try THIS:

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

I get the error message "Your shut_down function threw the following error: maximum recursion depth exceeded".

But THIS gives me the correct solution.

def shut_down(s):
        if s == "yes":
            return "shutting down"
        elif s == "no":
            return "shutdown aborted"
            return "sorry"

I'm wondering why I can't directly plug in "yes" and "no" for s in the shut_down() function? Why do I need to define s as "yes" or "no" instead?



Nevermind. I got it: calling shut_down() again in if doesn't evaluate to a boolean. I'll leave this up in case someone else has the same issue.


Calling a function from within the function is called recursion. We generally do not do this without strict intention and careful monitoring to prevent an infinite loop, such as above.

The correct code uses the parameter, s, as it should.

If you copied this code, then you get your first lesson--using code you do not understand is a definite no-no.


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