0

I was learning Python from Codecademy.

There's a question:

Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, we'd use s for string). The shut_down function should return 'Shutting down...' when it gets 'Yes', 'yes', or 'YES' as an argument, and 'Shutdown aborted!' when it gets 'No', 'no', or 'NO'.

In response to this, I wrote this:

def shut_down(n):
    p=n.lower()
    if p=="yes":
        return "Shutting down..."
    elif p=="no":
        return "Shutdown aborted!"

But, when I try to run it, the following error occurs:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'yes' is not defined*

Please help me...is there some kind of mistake in my code?

1
  • 3
    I'm guessing that you put yes instead of "yes" in the first line of your file, but you didn't show it so hard to say for sure... Advice: READ what the compiler says :) Commented Aug 25, 2013 at 13:58

2 Answers 2

3

It is kinda hard to tell exactly what is going on since you didn't include line 1 (the line of the error). However, judging by my personal experience, I think your problem is one of two things:

1) You are on Python 2.x and using input when you should be using raw_input:

>>> input()
yes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'yes' is not defined
>>> raw_input()
yes
'yes'
>>>

2) On the first line you have yes when you should have 'yes':

>>> yes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'yes' is not defined
>>> 'yes'
'yes'
>>>

In both cases (and any I missed), you are treating "yes" as a defined variable instead of a string.

Sign up to request clarification or add additional context in comments.

1 Comment

codecademy.com/courses/python-beginner-c7VZg/4/… Check this out... i tried everything but it's not working...
1

The above code works fine. May be the problem is, while you are calling the function, you have used shut_down(yes) instead of shut_down('yes')

1 Comment

actually there is a app that automatically test the code... so when i submitted it shows an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.