if statement – Python Invalid syntax in elif

if statement – Python Invalid syntax in elif

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 isnt 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:

    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 🙂

if line[0] == 0:
    print graph G%d {n, (count)
    count +=1
elif line[0] == 1:

It seems that you accidentally missplaced your first elif. In that state it matches no ifs so you get an error.

if statement – Python Invalid syntax in elif

Leave a Reply

Your email address will not be published. Required fields are marked *