1

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***
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • So it allows you to enter an input and then it throws the syntax error? Some more code/screenshot of the error might be helpful – Jonathan Porter Feb 24 '18 at 03:34
  • Sure. I'll do that. It might be a little hard to understand. Is that fine? –  Feb 24 '18 at 03:44
  • @Jonathan Porter Wait... the error is in that line alone. That particular line alone if run in sublime text's REPL, it generates a syntax error. I have posted a screenshot of the error in my question now. Please look at it. –  Feb 24 '18 at 03:50

1 Answers1

2

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.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • I did that. The version is 2.7 but the code still results in a syntax error when I use the conditional statement that you provided. Anything else I can do? –  Feb 24 '18 at 03:43
  • @BOi: It wouldn't result in any error if you put that at the top level of your file, near the top of the file (before using the `input` function). You've missed something. – ShadowRanger Feb 24 '18 at 03:48
  • I'm sorry but that's exactly what I did. I put i right at the top of the file. However, it still generates an error. I'm not sure why. –  Feb 24 '18 at 03:54
  • Also I had an input() just before the line i have added in my answer. It executed without any errors. –  Feb 24 '18 at 03:59
  • @BOi: You entered legal Python code for the first one, that's all. The error in your screenshot is *exactly* what you'd expect from Py2 `input`. It tried to `eval` the string `"902 92 29"` as if it were a Python expression, and the error occurs when it sees the `92` separate from the `902`, with no operator in between. You need to post a [MCVE] in your question that shows what you've got. If you redefined `input` to `raw_input` in the same (or an outer) scope of where you use `input`, before you call it, this problem **would not happen**, and I can't psychically intuit what you've done wrong. – ShadowRanger Feb 24 '18 at 04:04
  • Oh. I see what you mean. It worked now. Thanks a lot. I misunderstood what you said. I really appreciate your help. Sorry about the misunderstanding. –  Feb 24 '18 at 04:08
  • I have another question. I have edited the previous question. Would greatly appreciate it if u answered. –  Feb 24 '18 at 05:33
  • @BOi: That's a completely different question; don't change your existing question completely after the fact, make a new question focused solely on the new problem. That said, two seconds on Google gave me this: https://coderwall.com/p/nhq2gg/setting-up-sublimerepl-with-python3 – ShadowRanger Feb 24 '18 at 05:36
  • I tried that but I cant seem to find main.sublime-menu in my computer. –  Feb 24 '18 at 06:01