What exactly does this actually do?

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

what exactly does this do?

It’s a function that takes one parameter, “s”. When the function is called and value for “s” (think of “s” as a placeholder for whatever value one would enter) is entered, the logic in the function is applied. if, elif, else are test expressions. If the first condition evaluates to True, then the result of the return is executed, if not, and it evaluates to False then the next expression is evaluated, and so on.
Further…If s, is equal to “yes”, the return value is “shutting down”, else if, s is equal to ‘no’, then the shutdown is aborted, and anything other than those two arguments for “s” is input, then the return value is “sorry.”

Also remember that indentation is essential in Python b/c it tells the interpreter what code blocks to run. If you don’t indent, you’ll get an error.

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

print(shut_down('yes'))

Shutting down
1 Like

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