0

I'm new to Python, and I'm trying to compile a VERY simple code in Python (using Atom editor), and after I run this code:

name = input("Name:")
print(f"hello, {name}")

And after typing in any name (in this case I simply typed in n, I get this error message:

Name:n

Traceback (most recent call last):
  File "hello_name.py", line 1, in <module>
    name = input("Name:")
  File "<string>", line 1, in <module>
NameError: name 'n' is not defined

I hope you can help! Thanks!

4
  • 3
    This is inPython-2.x I assume? In Python-3.x this works fine. Commented Jun 29, 2020 at 22:37
  • 1
    I don't know... How can I check it? And also how to update it in Atom. LOVE if you could help Commented Jun 29, 2020 at 22:38
  • 2
    In Python-2.x input(..) is equivalent to eval(raw_input(..)) so it evaluates the input as if it is a Python expression. But anyway, Python-2.x is no longer supported since January 1, 2020 Commented Jun 29, 2020 at 22:39
  • 1
    well Python-3.x is not an "upgrade" of Python-2.x. The two are actually two different languages (that look quite similar). On a Linux system, you run Python-3.x with $ python3. Commented Jun 29, 2020 at 22:41

2 Answers 2

2

You need to use raw_input instead of input, because input runs eval on the input. (Only in python 2, in Python 3 input has the same behavior of raw_input and raw_input is removed)

What that means is that after getting your input, python would evaluate your input as if it was code.
Try entering "3+2*5-2" in the input and see the output.

(Note that writing something like:"x = 5" won't work, because eval just evaluates an expression, like y+3 (assuming y is defined) and it does not actually run the code)

(But really, you should use python 3 if you are just learning Python and you are not already used to python2)

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

5 Comments

FYI, this is an Atom related issue. I ran this on python 3.7, and it works perfectly.
Can you try the same but not using atom? (I am suspicious that Atom did not switch interpreter.)
Ok! Do you have any other recommendations for other good compilers for Atom (I'm doing cs50's course, so I have the IDE, but I also want to have a desktop based compiler like Atom)
You mean other code editors? you can use Visual Studio Code. But did it work? running the same code not from Atom?
Yes! I just ran it on Visual Studio Code, but I get the same error (because before I ran it on an IDE). I dowloaded everything!!! I don't know why but this exact piece of code DOESN'T work in my Visual Studio Code
-1

yes its right i also phase a problem like that and my code is

name = input("enter you name: ")
print(name)

so i enter my name Anshu then it show this error

Traceback (most recent call last): File "/home/anshu/Projects/coding/python/dictnory_problem.py", line 6, in search = input("enter your keyword: ") File "", line 1, in NameError: name 'anshu' is not defined

then i change my code i use

name = raw-input("enter your name")
print(name)

3 Comments

sorry for that its raw_input
This is just saying the same as the accepted answer... with typos added.
ya you can say this but it work in condition

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.