Functions. Function failed and I don't understand why!


#1

Hi, I'm having trouble with this exercise, and I don't see what I've done wrong. T console returns NONE when I submit the code, but I get an error message that says:

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

def shut_down(s):
return s

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

Could someone please help?

`


#2

What's that? In the instructions, they ask for you to return with the string provided instead of
shut_down (" ") .


#3

When I do it like this :

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

I get the error message:
File "python", line 5
shut_down "Shutting down"
^
SyntaxError: invalid syntax

While when I tried to do
def shut_down(s):
return s

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

I got the same error message as in the first message. I'm very confused...


#4

A function ends when the return keyword is reached. After defining your function, you returned s back to your function def shut_down(s):, but in your if/elif/else statements, they only return when one of them is True, and when that happens, your function ends there. Do you see your error in your code?


#5

It worked now :slight_smile: Thanks !
def shut_down(s):
return shut_down

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

#11

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