Review: Functions. Function call out


#1



Hello everybody!
I have some problem with a task, and google have not helped me a lot.
I do not understand what is wrong.
This exercise:

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


Compiler eeror:
Traceback (most recent call last):
File "python", line 10, in
File "", line 1, in
NameError: name 'Yes' is not defined


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


s = input("Do you want to turn off? Write yes or no.").lower()
shut_down()
print (shut_down())


#2

in python2, use raw_input unless you have a very good reason to use input


#3

Hello,
Thank you for fast respond!

I have corrected code:

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


s = raw_input("Do you want to turn off? Write yes or no.").lower()
shut_down(s)
print (shut_down)

But complier still says:
Traceback (most recent call last):
File "python", line 11, in
File "python", line 2, in shut_down
TypeError: 'unicode' object is not callable


#4

here:

if s() == "yes":

you made s (which contains your string) a function call, why? Why not simply check if s equals "yes"


#5

Oh, I have not marked it, you are correct, sorry for such silly mistake.

But by the way, it is still showing strange result.

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


s = raw_input("Do you want to turn off? Write yes or no.").lower()
shut_down(s)
print (shut_down)

It showing as a result:

None


#6

here:

shut_down(s)
print (shut_down)

replace this by:

print shut_down(s)

this is python2, no need for parentheses with print. Now print will print what the function returns


#7

I have corrected, but result is the same:

<function shut_down at 0x7fb06868d9b0>
None

#8

can i see an updated version of your code?


#9

Yes, of course. Sorry for not including it at first time.

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


s = raw_input("Do you want to turn off? Write yes or no.").lower()

print (shut_down)

#10

you should have:

print shut_down(s)

this will call the function, and print the returned result


#11

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