Cant find mistake in Functions Review


#1



def shutdown(s):
    return s
if yes():
    return("Shutting down")
elif no():
    return("Sborting shut down")
else:
    return("Sorry")


#2

changed some code

def shut_down(s):
    return s
if s == yes():
    return("Shutting down")
elif s == no():
    return("Sborting shut down")
else:
    return("Sorry")

i get the error
_ File "python", line 4
SyntaxError: 'return' outside function_


#3

a function ends the moment a return keyword is reached. you should also nest the conditions inside the function:

def example():
  if True:
     print "if inside the function"

you will need to change some indent in your code (see my example)

also, yes and no are just strings, use quotation marks and no parentheses.


#4

Ok I have changed some things using your advice, but I get the error Oops try again. Did you create a function called shut_down?

def shut_down(s):
    if yes:
        return "Shutting down"
    elif no:
        return "Aborting shutdown"
    else:
        return "Sorry"

#5

i added quotation marks for "yes" and "no" just realised

error now:
Oops, try again. Your function failed on the message no. It returned 'Shutting down' when it should have returned 'Shutdown aborted'


#6

your s == was very good, i just told you to convert yes() and no() into string, no need to remove s==, which based of your latest code, not your first one


#7

worked! Thank you for the help.


#8

do you also understand why it worked?


#9

yes, s is equal to a string and strings need to be in "" and if you dont have it, it will error.


#10

so now you could add a function call (at the end of your code, with no indent) to see your function in action:

print shut_down("yes")

this will print Shutting down


#11

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