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?
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.
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.
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.
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.
Because you left out a closing brace
w.rules.append(Rule(change[:5],change[5]) )
The previous line, w.rules.append(Rule(change[:5],change[5])
, is missing a close paren.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With