Dermot Johnson
Dermot Johnson

Reputation: 79

I keep getting this error when i run my functions:- SyntaxError: unexpected EOF while parsing

I don't know why I am getting a error message when i run addItem() saying:

SyntaxError: unexpected EOF while parsing

I have all my reading and writing to files closed, and I'm using raw_input functions. I think something is wrong with my readfrom() function, but I don't know what exactly.

import ast


def writeTo():

    itemx=",".join(map(str,itemx))
    with open("ListItemz.txt","a") as filewrite:
        filewrite.write(str(itemx)+"\n")
    filewrite.close()

def readfrom():

    fileread=open("ListItemz.txt","r")
    fr=fileread.readlines()
    fr=fr[len(fr)-1]
    itemx=list(ast.literal_eval(fr))
    fileread.close()

itemx=[]

def addItem():

    global itemx
    if  itemx==[]:
        itemx=[]
    else:
        """
        about to read file:
        """
    readfrom()
    dowhile="y"
    while dowhile =="y":
        item=raw_input("please enter item discription, or barcode No. ")
        if itemx!=[]:
            for y in itemx: 
                if item==y[0] or item==y[1]:
                    raise TypeError("This Item is already added")
        case=int(raw_input("Enter how much holds in one (1) case: "))
        caseNo=int(raw_input("how many cases are there?  "))
        for i in stockList():
            if item==i[1] or item==i[0]:
                itemx+=[[i[0],i[1],case,caseNo]]
                print "ITEM ADDED"
        dowhile=raw_input("Do you want to add another?(Y/N) ")
        """
        about to write itemx to a file:
        """
        writeTo()
    return itemx

Upvotes: 2

Views: 121

Answers (2)

Dermot Johnson
Dermot Johnson

Reputation: 79

the file i wrote to (ListItemz.txt) had complications so i just deleted very thing and started a fresh.

Upvotes: 2

jcoppens
jcoppens

Reputation: 5440

I don't think the error comes from the code above. The parsing and syntax refer to the Python parser trying to read your program, not from the reading or writing you do. As the program above is not complete - there is no main program - it's difficult to see where the error could originate. Or maybe there is a main program, but the indentation is off.

There's also this strange construct:

if  itemx==[]:
    itemx=[]

Is that really correct?

To try and pinpoint the problem, you can use the (under-appreciated) Python debugger (pdb). Add an import pdb at the top, and insert a line where you want to stop the program:

pdb.set_trace()

You can then do:

n<enter> to advance a line of code
p variable<enter> to see the value of a variable
c<enter> to continue at full speed
... and a lot more - see the pdb manual.

Just advance through the program till the error pops.

Upvotes: 1

Related Questions