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
Green
  • 577
  • 3
  • 10
  • 28

3 Answers3

5

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

or

simply use raw_input()

vks
  • 67,027
  • 10
  • 91
  • 124
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'
lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30
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.

shaktimaan
  • 1,769
  • 13
  • 14
  • oh!!! thanks shakthimaan... now I got the difference between variable name and string value while passing as input.... – Green Jul 01 '15 at 06:08
  • @Green Also this is only true for `Python 2.x`. In `Python 3.x` `input()` returns strings. – shaktimaan Jul 01 '15 at 06:58