-1

Whenever I try to execute this code:

name = input("What's your name?")
print("Hello World", name)

By running the command python myprogram.py on the command line, it gives me this error:

What's your name?John
     Traceback (most recent call last):
         File "HelloWorld.py", line 1, in <module>
             name = input("What's your name?")
         File "<string>", line 1, in <module>
     NameError: name 'John' is not defined

It asks me the name but as soon as I type it and press enter it crashes, what does the error mean? Thanks.

4
  • 2
    The error means that you're using Python 2.x. Commented Nov 21, 2017 at 17:02
  • @Carcigenicate I know, this is like a new record from what I've seen Commented Nov 21, 2017 at 17:10
  • This seems to be already answered: stackoverflow.com/questions/16179875/… Commented Nov 21, 2017 at 17:12
  • @Greg Yup, it's already been answered 100 times over. Commented Nov 21, 2017 at 17:12

2 Answers 2

1

In Python 2 you should use raw_input instead of input in this case.

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

9 Comments

Ok that solved the problem, however I checked with dnf (I'm using Fedora 25) and I do have Python 3.5.4 so I should be able to use input instead of raw_input
@BeppeChiari Having Python 3.x doesn't mean you're using Python 3.x.
can you check you actual version using python --version ?
Did it, it says Python 2.7.13, how do I change that? I'm using Fedora 25
@BeppeChiari Please do your own research.
|
0

Assuming you are using python 2 when you enter just (John) it interprets that as variable. You will either need to enter ("John"), forcing it to see a string, or use name = raw_input() in the first line.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.