Why is the "else" line giving an invalid syntax error?

Mateusz Korycinski picture Mateusz Korycinski · Mar 23, 2011 · Viewed 68.3k times · Source

I'm having this error:

File "zzz.py", line 70
    else:
       ^
SyntaxError: invalid syntax

The line which causes the problem is marked with a comment in the code:

def FileParse(self, table_file):
    vars={}
    tf = open(table_file, 'r')
    for line in tf:
        if line.startswith("#") or line.strip() == "": pass
        elif line.startswith("n_states:"):
            self.n_states = str(line[9:].strip())
        elif line.startswith("neighborhood:"):
            self.neighborhood = str(line[13:].strip())
        elif line.startswith("symmetries:"):
            self.symmetries = str(line[11:].strip())
        elif line.startswith("var "):
            line = line[4:]
            ent = line.replace('=',' ').\
            replace('{',' ').\
            replace(',',' ').\
            replace(':',' ').\
            replace('}',' ').\
            replace('\n','').split()
            vars[ent[0]] = []
            for e in ent[1:]:
                if e in vars: vars[ent[0]] += vars[e]
                else: 
                    vars[ent[0].append(int(e))]     
        else:
            rule = line.strip().split(",")
            for k in vars.keys():
                if k in rule:
                    for i in vars[k]:
                        change = rule.replace(k, i)
                        change = [int(x) for x in change]
                        w.rules.append(Rule(change[:5],change[5])

                else: # line which causes the problem

                    rule = [int(x) for x in rule]
                    w.rules.append(Rule(rule[:5],rule[5]))                  
    tf.close()
    self.parse_status "OK"
    return w.rules

w.rules is variable which is assigned to "World" class.

To be honest I have no idea why I get this. Before everything was fine and now that error shows up after adding some extra instructions in other indented blocks.

Any ideas?

Answer

kurumi picture kurumi · Mar 23, 2011

Because you left out a closing brace

w.rules.append(Rule(change[:5],change[5]) )