pythonpython-2.7

Basic Python: exceptions.NameError


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! :)


Solution

  • 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