The accepted answer provides the correct solution and @ghostdog74 gives the reason for the exception. I figured it may be helpful to see, step by step, why this raises a NameError
(and not something else, like ValueError):
As per Python 2.7 documentation, input() evaluates what you enter, so essentially your program becomes this:
username = input('...')
# => translates to
username = eval(raw_input('...'))
Let's assume input is bob
, then this becomes:
username = eval('bob')
Since eval()
executes 'bob' as if it were a Python expression, your program becomes this:
username = bob
=> NameError
print ("Hello Mr. " + username)
You could make it work my entering "bob" (with the quotes), because then the program is valid:
username = "bob"
print ("Hello Mr. " + username)
=> Hello Mr. bob
You can try it out by going through each step in the Python REPL yourself. Note that the exception is raised on the first line already, not within the print statement.