Reputation: 515
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!
Upvotes: 0
Views: 1365
Reputation: 381
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)
Upvotes: -1
Reputation: 592
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)
Upvotes: 2