Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `input` in Python 3 throwing NameError: name... is not defined [duplicate]

I have a string variable test, in Python 2.x this works fine.

test = raw_input("enter the test") 
print test

But in Python 3.x, I do:

test = input("enter the test") 
print test

with the input string sdas, and I get an error message

Traceback (most recent call last):
 File "/home/ananiev/PycharmProjects/PigLatin/main.py", line 5, in <module>
    test = input("enter the test")
 File "<string>", line 1, in <module> 
NameError: name 'sdas' is not defined
like image 722
DenisAnanev Avatar asked May 18 '13 15:05

DenisAnanev


People also ask

How do I fix NameError is not defined in Python?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

How do you correct a NameError in Python?

To specifically handle NameError in Python, you need to mention it in the except statement. In the following example code, if only the NameError is raised in the try block then an error message will be printed on the console.

What causes NameError in Python?

In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.


1 Answers

I got the same error. In the terminal when I typed "python filename.py", with this command, python2 was tring to run python3 code, because the is written python3. It runs correctly when I type "python3 filename.py" in the terminal. I hope this works for you too.

like image 108
Kevin Wright Avatar answered Sep 19 '22 20:09

Kevin Wright