Followed the example, but still get error


#1



Hi,

I'm working on a function exercise, and I'm getting an error, even though I'm following the sample code.


https://www.codecademy.com/courses/python-beginner-c7VZg/4/1?curriculum_id=4f89dab3d788890003000096#

I'm getting this error: "NameError: name 'yes' is not defined." I understand that it's not defined yet, but I don't understand why it works in the sample code but not in mine. Please help!

It's supposed to return "Shutting down."


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

MY CODE:

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


#2

Well, the example is not entirely helpful in that it has undefined functions, happy() and sad(). Easy to see how emulating the example results in exceptions being raised. The model is there, though.

Let's add an extra function to handle inputs...

def shut_down_prompt():
    return raw_input("Do you want to shut down the system?")

command = shut_down_prompt()
if command == 'yes':
    shut_down("Shutting down.")
elif command === 'no':
    shut_down("Shutdown aborted.")
else:
    shut_down("Sorry.")

But we could write everything in one function (with or without the prompt function)

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

print shut_down('yes')

Fortunately this exercise has a lot of wiggle room and will accept almost any code that outputs what is expected. Consider this variation...

def shut_down(s):
    s = s.lower()
    return "Sorry" if s not in "yesno" else \
    "Shutting down" if s == "yes" else "Shutdown aborted" 
    
print shut_down('yes')
print shut_down('no')
print shut_down('maybe')
print shut_down('1234')

Output

Shutting down
Shutdown aborted
Sorry
Sorry
None

#3

@kdough03,
You should be aware that if you use the return statement
a result is returned but the function is immediately EXITED
and any following statements will never be executed.....!!!

If you are using s as parameter
this s parameter is used as a =local= variable throughout the shut_down() function
and you will want to use the condition's

s == "yes"
s == "no"

in your IF ELIF statement structure....

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )

#4

MTF and leonhard,

Thanks so much for your help. I thought that sample code looked strange within the context of that particular test. I had tried if and elif s == 'yes' and 'no' too, but evidently did get it quite right.

Kate