Review Functions: "Plugging In" for Functions


#1




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


When I try THIS:

def shut_down(s):
    if shut_down("yes"):
        return "Shutting down"
    elif shut_down("no"):
        return "Shutdown aborted"
    else:
        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"
        else:
            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?

Thanks!


#3

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.


#4

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.


#5

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