pythonsublimetext3sublimetextpython-2.x

Python code with Input() given syntax error in Sublime REPL, but works with Python Shell


I typed a piece of code in python 3 and the python shell runs it extremely well and gives the right output. I transfered this code to Sublime Text 3, downloaded the package sublime REPL, and I used it to run my code. However, it tells me there is a syntax error when I run it and enter an input in Sublime using REPL. Here's the line where it finds the syntax error:

dataGiven = sorted([float(x) for x in input("Enter data separated by a space (At least 3 values of data must be entered) ").split()])

But, there is no syntax error because the python shell runs the code properly. Please help. I find Sublime text to be really helpful but it just wont run properly because of this input. I am willing to post a bigger segment of the code if you need it. Thanks in advance.

EDIT

screenshot of error:

Traceback (most recent call last):
  File "Untitled.py", line 708, in <module>
    dataGiven = sorted([float(x) for x in input("Enter data separated by a space (At least 3 values of data must be entered) ").split()])
  File "<string>", line 1
    78 67 45
        ^
SyntaxError: invalid syntax

***Repl Closed***

Solution

  • Sounds like Sublime is running it using a Python 2 interpreter. input tries to eval the string entered in Python 2, which is about the only way to get SyntaxError at runtime (because most arbitrary user entered strings aren't syntactically legal Python code). Try changing your code to:

    import sys
    print(sys.version_info)
    

    which will tell you the actual version it's being run as. If it's Python 2, and you need it to run on Python 2 like it does on Python 3, switch to the raw_input function, or to mask away the version differences, add:

    if sys.version_info < (3,):
        input = raw_input
    

    to the top of your file to replace input with raw_input on Python 2, so it behaves like it does on Python 3.