Reputation: 597
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
Upvotes: 1
Views: 1056
Reputation: 1857
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.
Upvotes: 1
Reputation: 67988
When passing a string use "
or put hello
inside "
."hello"
something like this.
or
simply use raw_input()
Upvotes: 5
Reputation: 3782
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'
Upvotes: 2