I have string variable test, In Python 2.7 this works fine.
test = raw_input("enter the test")
print test
But in Python 3.3. I use
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 test = input("enter the test")
File "", line 1, in NameError: name 'sdas' is not defined
You're running your Python 3 code with a Python 2 interpreter. If you weren't, your
print
statement would throw up aSyntaxError
before it ever prompted you for input.The result is that you're using Python 2's
input
, which tries toeval
your input (presumablysdas
), finds that it's invalid Python, and dies.In operating systems like Ubuntu python comes preinstalled. So the default version is python 2.7 you can confirm the version by typing below command in your terminal
if you installed it but didn't set default version you will see
in terminal. I will tell you how to set the default python version in Ubuntu.
A simple safe way would be to use an alias. Place this into ~/.bashrc or ~/.bash_aliases file:
After adding the above in the file, run the command below:
source ~/.bash_aliases
orsource ~/.bashrc
now check python version again using
python -V
if python version 3.x.x one, then the error is in your syntax like using print with parenthesis. change it to
I'd say the code you need is:
Otherwise it shouldn't run at all, due to a syntax error. The
print
function requires brackets in python 3. I cannot reproduce your error, though. Are you sure it's those lines causing that error?sdas is being read as a variable. To input a string you need " "
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.