Mayank Jain
Mayank Jain

Reputation: 2564

Code not compiling in Python3

I wrote a code and it is getting compiled on my PC with Python3. But Showing error while uploading to Codechef server. Please suggest, I am coding for the first time in Python 3.

Traceback (most recent call last):
  File "/run-ls7W2DcLmzUs9GNKbLGN/solution.py", line 41, in <module>
    l,r,k=map(int,input().split())
  File "<string>", line 1
    9 23 1
       ^
SyntaxError: invalid syntax

Upvotes: 0

Views: 1869

Answers (1)

mgilson
mgilson

Reputation: 309821

You're using python2.x which evaluates the string you enter for input. Change the function from input to raw_input and you should be all set.

If you want the code to work for both python2.x and python3.x, you could do a simple little hack like this at the top of your script:

try:
    #This raises `NameError` on python3.x since `raw_input` is renamed to `input`
    input = raw_input
except NameError: 
    pass

It's not pretty, but it works (and I've used things like this on occasion). Ultimately, this shadows the builtin input on python2.x, but that's really not a big deal. You probably don't want to be using that builtin for any serious coding anyway.

Upvotes: 4

Related Questions