Review : functions


#1

Hi I am a bit confused with tis section

there this example:
def speak(message):
return message

if happy():
speak("I'm happy!")
elif sad():
speak("I'm sad.")
else:
speak("I don't know what I'm feeling.")

First why its written :if happy() (where this happy come from???

This is what I have done so far but its not write. can someone advice please? thanks
def shut_down(s):
return s

if shut_down("yes"):
    return "shutting down"
elif shut_down("no"):
    return "shutdown aborted"
else:
    return "Sorry"

#2

your program has a different design then the program in the example. Please follow the instructions, the example just demonstrate certain code concepts


#3

i am clearly not understanding the instructions, thats my problem.

can I write a random condition?
lf 5 < 6 == "Yes" ??


#4

this are the intructions:
Then, if the shut_down function receives an s equal to "yes", it should return "Shutting down"
what i am supposed to do with this?


#5

this sounds like a condition to me:


#6

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

this is what the system says to me, ive tried different ways and its not working, i am clearly missing something very simple of understanding

if you could elaborate an example or to direct me that would be good


#7

can i see an updated version of your code?


#8


#9

you shouldn't just make random conditions, the exercise tells you what the condition should be:

and if this condition is true, what should be returned:


#10

i've tried:

if s == "yes":
return shut_down("shutting down")

if s == "yes":
return "shutting down"

If shut_down("yes"):
return "shutting down"

If shut_down == "yes":
return "shutting down"


#11

one of these is correct. Which one do you think it is? and why?

Can i see your full code again?


#12

def shut_down(s):
return s

if s == "yes":
    return "shutting down"
elif s == "no":
    return "shutdown aborted"
else:
    return "Sorry"

#13

a function ends the moments a return keyword is reached. So when does your function end?


#14

meaning if I delete the sentence return s it works, but is this the right answer?

def shut_down(s):

if s == "yes":
    return "shutting down"
elif s == "no":
    return "shutdown aborted"
else:
    return "Sorry"

#15

yes, its the right answer. Why are you doubting?


#16

3 posts were split to a new topic: Review function


#17

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