Review Functions [SOLVED + CORRECT CODE EX.]


#1

17. Review: Functions

I've looked at quite a few other topics and couldn't find any answers, can anyone help me, and tell me why my code is wrong? Is it the indentation? 'Cause if (s) == "yes", or line 2, keeps getting "Syntax Error: invalid syntax"

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

EDIT: The correct code is at 7/7, down at the bottom, read the rest to see problems with indentation and capitalization


#2

For one, you don't need the s in (). Just check if s == "yes". Second, your indents for your elif and else statements are wrong. They should be in line with the if statement.


#3

I fixed some thing before I saw your comment, how's this? It tells me
else:
return ("Sorry")
or line 6 of my code, has a "Syntax error: invalid syntax"

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

#4

Your else is still out of line. When that code runs, the function runs fine, but then after the function, it tries to run else. Else without an if doesn't mean anything which is throwing a syntax error.


#5

I realigned it and now it says:
"Oops, try again. Your function failed on the message yes. It returned 'Sorry' when it should have returned 'Shutting down' "

Fixed code:

def shut_down(s):
    if s == ("Yes"):
        return "Shutting down"
    elif s == ("No"):
        return "Shutdown aborted"
    else: 
        return ("Sorry")

#6

Notice the capitalization. It says it failed on "yes", not "Yes". That means that you should be checking if s is yes in lowercase. "Yes" != "yes"


#7

Thank you so much man honestly, you were very helpful, and that's a great hint, it's like finding the answer in the question.

For others who are stuck, here is the correct coding (correct indentations, capitalization etc):

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

#8

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