0

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".

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

2 Answers2

2

In Python 2.x, input tries to evaluate the input as a Python expression. You want to use the raw_input function instead:

# 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.)

chepner
  • 497,756
  • 71
  • 530
  • 681
1

Python 2, use raw_input:

name = raw_input(' Enter name: ')

Python 2's input function is basically Python 3's eval(input(..))

Python 3, python 2's input is removed,

Only keeping raw_input which is renamed to input

U13-Forward
  • 69,221
  • 14
  • 89
  • 114