17. Review: Functions (printing)


#1

I can't seem to print the results it returns. The code is fine, it works. Just the printing part that I can't figure out.


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

I get this error:

Traceback (most recent call last):
File "python", line 16, in
NameError: name 'yes' is not defined


#2

I tried this, and it wouldn't work as well:

raw_input("Please write Yes or No:")

def shut_down(s):
return shut_down

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

elif s == no:
    return "Shutdown aborted"
    print "Shutdown aborted"

else:
    return "Sorry"
    print "Sorry"

#3

Alright, first of all, your input needs to be a string print shut_down("yes").
Second of all, your conditions needs to deal with strings and not undefined variable names...which is what Python read your yes and no as in your conditions.
Simply change them to this:

#rest of your code
    if s == "yes":
        return "Shutting down"
            
    elif s == "no":
        return "Shutdown aborted"
#rest of your code

You can follow the code above this comment for the print statement. But your error specifies what I talked about.

ALSO: remove that return shut_down before your conditional statements. That return statement isn't going to do anything other than give you an error (because it's missing your function's parameter).
@guiioli
You don't need that raw_input as your function doesn't use it. Also please refer to the answer above as well.


#4

Thanks for the insight, but it still didn't work.

def shut_down(s):
return shut_down

if s == "yes":
    return "Shutting down"
    print "Shutting down"
elif s == "no":
    return "Shutdown aborted"
    print "Shutdown aborted"
else:
    return "Sorry"
    print "Sorry"

print shut_down("yes")

It shows me this:

Do I have to assign a value to s? Something like s = "yes"?


#5

Why do you have a return shut_down before your if statements?
I updated my comment above about this.


#6

It was there because somehow the lack of it was causing some error on the codeacademy code reader. Probably I was getting an error because 'yes' and 'no' weren't strings. Anyway, I took it out and it worked.

Thanks a bunch. :smiley:


#7

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