4/17 Review: Functions help


#1

Hi there

i am stuck on Review: Functions
UNIT 4: FUNCTIONS


#2

Hi,

please tell us what the problem is. Do you get an error message? Or are the instructions unclear to you?


#3

the situation is unclear


#4

Perhaps start by describing the terms and concepts you don't yet understand. Please post a link to the first problem so we can discuss it and maybe get you started on your own.


#5

im having this error
Oops, try again. Your shut_down function threw the following error: maximum recursion depth exceeded
link https://www.codecademy.com/en/courses/python-beginner-c7VZg/4/1?curriculum_id=4f89dab3d788890003000096


#6

Please post your code so we can examine it.


#7

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


#8

got the answer from


#9

Would have been nicer if we could have worked it out. However, now that you have a solution, is there anything in it that you do not understand?

To see the output, print the call expression:

print shut_down('yes')
print shut_down('no')
print shut_down('maybe')

#10

Extra study and review:

Let's compare the present solution (which follows the instructions, btw) with another slightly different one that takes advantage of Python's syntax:

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

Compare to,

def shut_down(s):
    s = s.lower()
    if s not in "yesno":
        return "Sorry"
    return "Shutting down" if s=="yes" else "Shutdown aborted"

There is no point to be made here. This obviously does not follow the instructions, explicitly, though it does yield the same results, and even passes the SCT.

The last line is known as a Python ternary expression

Action  ->  if Condition  ->  else  ->  Alternate Action

We also make use of the built-in string function, .lower() to massage all inputs to lower case before testing.