Tommy Chan
Tommy Chan

Reputation: 79

input() causes unexpected EOF SyntaxError

I have written a return function for my group project. I am using python 3.4 and wrote this:

def readrouter(x, y):
        conn = sqlite3.connect('server.db')
        cur = conn.cursor()
        cur.execute("SELECT DISTINCT command FROM router WHERE
                     function =? or type = ?  ORDER BY key ASC",(x, y))
        read = cur.fetchall()
        return read;

a = input("x:")
b = input("y:")
for result in readrouter(a,b):
    print (result[0])

As my major member is using 2.7 and I need to follow his version now. After I re-input my .py into python 2.7 there is a error:

x:create vlan
Traceback (most recent call last):
  File "C:/Users/f0449492/Desktop/2015225/database.py", line 322, in <module>
    a = input("x")
  File "<string>", line 1
    create vlan
             ^
SyntaxError: unexpected EOF while parsing

Process finished with exit code 1

how to fix this bug?

Upvotes: 1

Views: 272

Answers (2)

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23203

As a follow up - to ensure compatibility with both Python branches you may use six .

Upvotes: 0

Raymond Hettinger
Raymond Hettinger

Reputation: 226221

In Python 2.7, replace input() with raw_input().

The former runs eval() on the input string and expects valid Python code as input. Your input create vlan isn't valid Python and can't be eval'ed. The latter just returns a string with no further processing.

Upvotes: 2

Related Questions