How to fix NameError in input() function in Python 3.x?

Question:

I am currently running Python 3.7.2 on Windows 10. I am trying to use this code print('Hello'+input()) but it’s not working. When I input a name, say, John, after Python has printed ‘Hello’, it gives me the following error message:

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
    John
NameError: name 'John' is not defined

I tried looking for solutions on the internet but most of them are talking about how input() should be raw_input() on Python 2.x but I couldn’t find any solution for Python 3.x. Any help would be much appreciated.

EDIT (add screenshot from comment):
enter image description here

Asked By: miniminimish

||

Answers:

From your screenshot, I think you’re simply testing it incorrectly.

wrong

After typing print('Hello'+input()) and pressing the Enter key, the console is already at the input() prompt (at the blank line), and is waiting for your input. But instead, you pressed the Enter key again, so input() receives an empty string and prints "Hello"+"" which just prints "Hello".

When you type “John”, it’s already treated as the next line of code (indicated by the >>>), which Python treats as a variable and it tries to print the value of John but that is clearly undefined, resulting in the error you see.

The correct way is, after entering print('Hello'+input()), just type John at the blank line, to pass it to input():

right

Answered By: Gino Mempin