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.
-
2The error means that you're using Python 2.x.Matthias– Matthias2017年11月21日 17:02:14 +00:00Commented Nov 21, 2017 at 17:02
-
@Carcigenicate I know, this is like a new record from what I've seenmiradulo– miradulo2017年11月21日 17:10:42 +00:00Commented Nov 21, 2017 at 17:10
-
This seems to be already answered: stackoverflow.com/questions/16179875/…Greg– Greg2017年11月21日 17:12:31 +00:00Commented Nov 21, 2017 at 17:12
-
@Greg Yup, it's already been answered 100 times over.miradulo– miradulo2017年11月21日 17:12:58 +00:00Commented Nov 21, 2017 at 17:12
2 Answers 2
In Python 2 you should use raw_input instead of input in this case.
answered Nov 21, 2017 at 17:03
Julien Salinas
1,1291 gold badge10 silver badges25 bronze badges
Sign up to request clarification or add additional context in comments.
9 Comments
overmach
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_inputmiradulo
@BeppeChiari Having Python 3.x doesn't mean you're using Python 3.x.
Julien Salinas
can you check you actual version using
python --version ?overmach
Did it, it says Python 2.7.13, how do I change that? I'm using Fedora 25
miradulo
@BeppeChiari Please do your own research.
|
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.
answered Nov 21, 2017 at 17:03
Isaac Little
361 silver badge7 bronze badges
Comments
lang-py