arjun arjun - 2 months ago 10
Python Question

Oops, try again. Your function failed on the message yes. It returned 'yes' when it should have returned 'Shutting down'

Here is my pyhton practice program:

def shut_down(s):
return s

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


the question given to me are:


  1. if the shut_down function receives an s equal to "yes", it should return "Shutting down"

  2. elif s is equal to "no", then the function should return "Shutdown aborted".

  3. if shut_down gets anything other than those inputs, the function should return "Sorry"


Answer

Couple of points:

  • Misplaced return statement. Should be at end.
  • if yes(): It is wrong. You want to compare function input with yes. It should be if s == 'yes':. Same for rest also.
  • Since you have written function definition as def shut_down(s):, it is expecting one argument. You should pass one argument while calling this function as shutdown(yes)
  • Once you called function and since your function has return statement, it will return some value, which you should catch like ret = shutdown(yes)

def shut_down(s):

    if s == "yes":
        r =  "Shutting down"
    elif s == "no":
        r =  "Shutdown aborted"
    else:
        r = "Sorry"
    return r


ret = shut_down("yes")
print (ret)