1

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

4
  • 1
    Is that all the code that get executed ? Otherwise, please provide the whole code snippet you are executing. Commented Jan 16, 2019 at 8:02
  • That's the whole code I'm trying to execute. Here's a screenshot of the whole thing for clarity: imgur.com/a/lX7K2wA Commented Jan 16, 2019 at 13:05
  • 1
    I would definitely check your python version. At the top of the script, do import sys and then print(sys.version) Commented Jan 16, 2019 at 13:12
  • @CalvinGodfrey Python version 3.7.2 because I got this 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] Commented Jan 16, 2019 at 13:19

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.