Review Function


#1

I'm trying to build a function in "Review Functions" page just exactly like this:

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

shut_down('no').

(And it says:" Make sure you calling 'return "Shutdown aborted"' when calling shut_down with 'no'.")
even the elif statement don't work.


#2

i see problems, you need a space between return and 'Shutting down' (same for the other returns)

make sure you indent is correctly, your if s == 'no' is currently nested inside your if s is yes statement.


#3

First of all please format your code. Here is a guide on how to use this forum including posting code as code:

As indents are part of the python syntax it is really crucial if we can't see your formatting because it is posted as a block of text.

And here it really seems to be important because according to your formatting this part:

if s== 'no':
        return'Shutdown aborted'
else:
        return'Sorry'

is part of this part:

if s =='yes':
        return'Shutting down'

so it is not an alternative to it but is only executed when this path is chosen and worse not even then because it comes after a return and you leave a function on return ignoring what comes after it. That is also the reason why elif won't work here, because elif would require an if and there is no if before the elif you're just inside of an if but that doesn't count.

So the code is the same but should rather look like this:

Spoiler Alert

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

shut_down('no')

And in this configuration you should be allowed to use elif but here it doesn't really matter because due to the return you're doomed to pick only one option anyway.


#4

I tried the indentation still the says the same result;"
Make sure you calling 'return "Shutdown aborted"' when calling shut_down with 'no'.
."
and my code is like:

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

#5

you did the spaces after return, you didn't fix the indent of your code. You can have to if statements which will both execute:

if condition1:
   do something
if condition2:
   do something else

or you can have nested if statements, in which case only if condition one is true, it can go on the to next if statement:

if condition:1
   do something
   if condition2:
      do something else

except condition two never gets executed in your case, since a function ends if a return statement is reached. Don't nest the if and else statement in you first if statemetn