12

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
pppery
  • 3,731
  • 22
  • 33
  • 46
DenisAnanev
  • 149
  • 1
  • 2
  • 5

7 Answers7

14

You're running your Python 3 code with a Python 2 interpreter. If you weren't, your print statement would throw up a SyntaxError before it ever prompted you for input.

The result is that you're using Python 2's input, which tries to eval your input (presumably sdas), finds that it's invalid Python, and dies.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
6

I'd say the code you need is:

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

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?

Noctua
  • 5,058
  • 1
  • 18
  • 23
1

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.

  • Or use the shebang in the python script :). More info, for example, here: https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take – s3n0 Mar 03 '19 at 15:06
1

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

python -V

if you installed it but didn't set default version you will see

python 2.7

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:

alias python=python3

After adding the above in the file, run the command below:

source ~/.bash_aliases or source ~/.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

test = input("enter the test")
print(test)
Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
0

sdas is being read as a variable. To input a string you need " "

none
  • 9
  • 1
0
temperature = input("What's the current temperature in your city? (please use the format ??C or ???F) >>> ")

### warning... the result from input will <str> on Python 3.x only
### in the case of Python 2.x, the result from input is the variable type <int>
### for the <str> type as the result for Python 2.x it's neccessary to use the another: raw_input()

temp_int = int(temperature[:-1])     # 25 <int> (as example)
temp_str = temperature[-1:]          # "C" <str> (as example)

if temp_str.lower() == 'c':
    print("Your temperature in Fahrenheit is: {}".format(  (9/5 * temp_int) + 32      )  )
elif temp_str.lower() == 'f':
    print("Your temperature in Celsius is: {}".format(     ((5/9) * (temp_int - 32))  )  )
s3n0
  • 596
  • 3
  • 14
-1

If we setaside the syntax error of print, then the way to use input in multiple scenarios are -

If using python 2.x :

then for evaluated input use "input"
example: number = input("enter a number")

and for string use "raw_input"
example: name = raw_input("enter your name")

If using python 3.x :

then for evaluated result use "eval" and "input"
example: number = eval(input("enter a number"))

for string use "input"
example: name = input("enter your name")
Mayank Jain
  • 436
  • 3
  • 13