0

I'm still new to python and I've encountered a problem that I have no idea how to deal with. I'm creating a program where the user inputs their name and then the program outputs "Hello" alongside their name. I've left the code below:

name = input("What is your name?")
print ("Hello " + name)

However, when I run this and enter a name it throws up this message:

exceptions.NameError: name 'bob' is not defined

In this case I inputted 'bob' by the way. Can anyone help me out? Thank you very much! :)

simonzack
  • 19,729
  • 13
  • 73
  • 118
lukeh38
  • 11
  • 3
  • input() evaluates what you enter, so essentially your statement becomes `name = input('...') => eval(raw_input('...')) => eval('bob')` this is the same as if you wrote `name = bob` => NameError. https://docs.python.org/2/library/functions.html?highlight=input#input – miraculixx Dec 28 '14 at 11:58
  • sorry, im still quite new to programming so coulf you explain that a bit more? sorry :) – lukeh38 Dec 28 '14 at 12:04
  • see my answer here: http://stackoverflow.com/a/27676242/890242 – miraculixx Dec 28 '14 at 12:22

1 Answers1

0

Please use raw_input() instead:

name = raw_input("What is your name?")
print ("Hello " + name)

Mostly the difference between them is that input(prompt) is equivalent to eval(raw_input(prompt)). You can read more details here

Paul Lo
  • 6,032
  • 6
  • 31
  • 36