Review function


#1

hey guys, i got a error and i dont understand why, here is my code

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

here is the error
File "python", line 4
elif (s == "no"):
^
SyntaxError: invalid syntax


#2

first of all, why would you put if after elif? you also wrote this wrong. Get rid of the parenthesis in every if, elif, and return statement.
That's

all of those.


#3

kk i get rid of the parenthesis, but its not solving my problem i put the parenthesis just for a question of lisibility


#4

i put if after elif because it the way they asking me to do it, and its not a big deal aniway i guess you can put many if elif it doesnt really matter

First, def a function, shut_down, that takes one argument s. Don't forget the parentheses or the colon!
Then, if the shut_down function receives an s equal to "yes", it should return "Shutting down"
Alternatively, elif s is equal to "no", then the function should return "Shutdown aborted".
Finally, if shut_down gets anything other than those inputs, the function should return "Sorry"


#5

heres code you are looking for... no need to include =! because (else) collects everything not specifically stated

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


#6

thanks, but still got an error with your code :
File "python", line 4
elif s == ("no"):
^
SyntaxError: invalid syntax


#7

Alright, well keep the parenthesis off, it's wrong to use them, thanks.
Instead of an if statement, what encompasses all other options?
Hint: AN ELSE STATEMENT

I will walk through this instructions with you.
First, def a function, shut_down, that takes one argument s. Don't forget the parentheses or the colon!
That's what you already have:

def shut_down(s):

Then, if the shut_down function receives an s equal to "yes", it should return "Shutting down"
Inside of the function:

def shut_down(s):
    if s == 'yes':
        return "Shutting down"

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

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

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

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

#8

Your code is wrong. You use parenthesis, and you're not supposed to. Like everywhere.
You absolutely do not want them here:

But you also definitely don't want them with your return statements.
Additionally, please do not just give people the code. Walk through it or explain it.


#9

worked perfect for me ms. "super user"


#10

hey thanks for your kind help. why do you think i don't understand what im writing lol ? it is easy to understand. the parentheses are here maybe because i use to put them with another language of programmation (c) and the syntax is just confusing me a bit


#11

yea it should work, its just a bug maybe im gonna try it on my own terminal thanks


#12

If you're talking about

I was replying to the other user, not you.
You obviously know what you're trying to do, but I just am adamant about the parenthesis because later on, they really can mess you up. They can mess up your program and hurt later on, because return is not a function. I don't know as much about if and elif statements, but I just try to utilize the syntax so it doesn't come back to bite me.
I'm glad you got everything to work. :slight_smile:
The syntax is tricky sometimes, even for people who are good at it. :slight_smile: have a good day.
If you need any more help, don't hesitate to make a topic.


#13

because she is a condescending prick that doesn't understand people write code differently. If parentheses help you understand what you are writing... it doesn't hurt even though it is repetitive.


#14

haha thanks dude, its just when you make an if statement in c for exemple :

if (*str != "python") // str is a variable contains string
{
print ("blabla")'; if the condition is true, then you print the string
}
if you dont put the parenthesis, you cant compile, and i use to write this language and the facility of python to do things paradoxaly is confusing me at the first sight but no worries im gonna handle this without probleme i guess


#15

That's a hasty judgement. I'm sorry if I hurt your feelings, but that seems a little self-righteous, don't you think?

I do understand that people write code differently, that's the point of having people write code rather than robots are computers. Just because the code works, it doesn't mean that it's right or that it will work every time. I'm not trying to be condescending, I'm just trying to make sure he was aware and had in his mind that it's not a good idea to go against syntax. I understand that parenthesis can help you visualize, but when learning a language, I think you should at least be told what bad habits are, and then choose for yourself whether or not to continue them.


#16

well thanks for that, i appreciate :slight_smile:


#17

I understand the parenthesis too. :slight_smile: I code primarily in Java, and the parenthesis are required. Same for JavaScript. I wasn't trying to make you feel insignificant, so I apologize if I did, I just was trying to get the point across that it's not really the correct syntax. I wish I knew more about it, I just might have been pushy, so my bad.
But we don't need to start drama, thanks for being kind. :slight_smile:


#18

nah its ok no worries, thanks for your help


#19

thanks, this helped me


#20

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