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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With