Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Invalid syntax in elif [closed]

The code below shows in invlid syntax in the first elif statement. I have checked and rechecked my code several times, but cant figure out how to solve the error.

fileHandle = open ( 'gra1.txt' )
count=0
count1=0
fileList = fileHandle.readlines()
for fileLine in fileList:
    line=fileLine.split()
    if line[0] == '0':
        print "graph G%d {\n", (count)
        count +=1
    elif line[0] == '1':
        print "} \n"
    elif line[0]=='':
        continue
    else:
        count1 += 1
        if count1==1: a=line[0]
        elif count1==2: relation=line[0]
        elif count1==3: b=line[0]
        else:
            print a, relation, b
            count1=0
fileHandle.close()
like image 929
abhisekG Avatar asked Jan 22 '14 08:01

abhisekG


1 Answers

Your elif is not indented properly...it should be indented the same way if is indented. Seeing the else block, it seems that you have by mistake indented the first if. Remember that elif/else should be preceded by an if always.

EDIT: corresponding to the edited question details: Why is the second else there? It isn't preceded by an if. I feel you need to get your conditions organized properly before writing the code.

One way to correct the code is to change this to an elif block:

else:
    count1 += 1
    if count1==1: a=line[0]
    elif count1==2: relation=line[0]
    elif count1==3: b=line[0]

You might want your indentation in Python to get better. Consider reading up a bit on that :-)

like image 133
gravetii Avatar answered Oct 10 '22 23:10

gravetii