Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 283
Mateusz Korycinski Avatar asked Mar 22 '11 23:03

Mateusz Korycinski


People also ask

How do I fix invalid SyntaxError?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

How do I fix invalid syntax in Python else statement?

The Python "SyntaxError: invalid syntax" is often caused when we use a single equals sign instead of double equals in an if statement. To solve the error, use double equals == if comparing values and make sure the line of the if statement ends with a colon.

Why is else an invalid syntax Java?

This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn't nested within your if statement.

Is else if valid in Python?

Python if...else StatementThe if..else statement evaluates test expression and will execute the body of if only when the test condition is True . If the condition is False , the body of else is executed. Indentation is used to separate the blocks.


2 Answers

Because you left out a closing brace

w.rules.append(Rule(change[:5],change[5]) ) 
like image 144
kurumi Avatar answered Nov 15 '22 05:11

kurumi


The previous line, w.rules.append(Rule(change[:5],change[5]), is missing a close paren.

like image 45
B_. Avatar answered Nov 15 '22 03:11

B_.