Having trouble assigning user input to a variable [duplicate]
Having trouble assigning user input to a variable [duplicate]
This question already has an answer here:
I am using the syntax
name = input(' Enter name: ')
print("hello " + name)
but every time I try to use a string with the name variable in it it says "name is undefined".
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2 Answers
2
Python 2, use raw_input
:
raw_input
name = raw_input(' Enter name: ')
Python 2's input
function is basically Python 3's eval(input(..))
input
eval(input(..))
Python 3, python 2's input
is removed,
input
Only keeping raw_input
which is renamed to input
raw_input
input
In Python 2.x, input
tries to evaluate the input as a Python expression. You want to use the raw_input
function instead:
input
raw_input
# input('...') is equivalent to eval(raw_input('...'))
name = raw_input(' Enter name: ')
However, if you are just starting out, you should be using Python 3, where the input
function behaves like Python 2's raw_input
function. (There is no function that automatically tries to evaluate the string; it was considered a bad design choice to provide such a function.)
input
raw_input