17. Review: Functions problem with the example


#1

I passed the exercise, but I need an explanation

this is the example code:

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."

Why do I get an error I try to add "return s" where the "return message" was in the example. Why is that there in the example?


#2

@terarunner93031,

The instructions for Exercise 17: Review: Functions ask the user to return specific strings based on the value that is passed to the shut_down function via the parameter, s. If your function simply returns s, it is merely returning the same information that was passed to the function, which is not what Codecademy is asking you to do.

Though, in the example function, speak, the same value is returned that is passed to the function, that is different from what the instructions specify that you should do with the shut_down function.


#3

In other words, the "return s" doesn't work merely because it's not the
part of the exercise?

Also, what was the interest of adding "return message" in the example? Just
to show the message that has been added?


#4

@terarunner93031 ,

Yes, you are right.

In my opinion, the speak function that is given is not a great example. Essentially, it is just there to show the mechanics of how a function can return a value. A function that simply returns the same information that it is given is not generally very useful.

Codecademy will only accept your code for this exercise if you return the strings that are specified in the instructions. For instance, you should begin as follows ...

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

The other two situations with their specified return values should be handled by an elif and an else block, respectively.


#5

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