1

I am using Python version 2.7.3. When I try to run the below code, an error is throwing,

>>> value = input("get value: ")

    get value: hello

    Traceback (most recent call last):

    File "<stdin>", line 1, in <module>

    File "<string>", line 1, in <module>

    NameError: name 'hello' is not defined
2

3 Answers 3

5

When passing a string use " or put hello inside "."hello" something like this.

or

simply use raw_input()

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

Comments

2

If you want input a number use input() If you want to input a string(like name) use raw_input():

>>> val = input('get value:')
get value:100
>>> val
100
>>> string = raw_input("get value:")
get value:hello
>>> string
'hello'

Comments

1

As suggested in previous answers use raw_input() instead of input(). Reason for this is that input() method interprets the value provided by user. If the user e.g. puts in an integer value, the input function returns this integer value. If the user on the other hand inputs a list, the function will return a list. Since you want to input a string. If you don't wrap your name into quotes, Python takes your name as a variable. So, the error message makes sense.

2 Comments

oh!!! thanks shakthimaan... now I got the difference between variable name and string value while passing as input....
@Green Also this is only true for Python 2.x. In Python 3.x input() returns strings.

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.