为什么“其他”是“其他”?给出无效语法错误的行?

发布于 2024-10-24 18:26:59 字数 1770 浏览 2 评论 0原文

我遇到此错误:

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

导致问题的行在代码中用注释标记:

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 是分配给“World”类的变量。

老实说,我不知道为什么我会得到这个。之前一切都很好,现在在其他缩进块中添加一些额外的指令后出现错误。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最丧也最甜 2024-10-31 18:26:59

因为你遗漏了一个右大括号

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

Because you left out a closing brace

w.rules.append(Rule(change[:5],change[5]) )
愛上了 2024-10-31 18:26:59

上一行 w.rules.append(Rule(change[:5],change[5]) 缺少右括号。

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

小情绪 2024-10-31 18:26:59

当你这样做时,还有另一个错字。您可能想要:

self.parse_status "OK"

成为:

self.parse_status = "OK"

While you're at it, there is another typo. You probably want:

self.parse_status "OK"

To be:

self.parse_status = "OK"
迷雾森÷林ヴ 2024-10-31 18:26:59

删除多余的空格/行并重新缩进 if/else 语句。这对我有用。

(我在这里尝试了其他解决方案,但没有一个有效。我的牙套很好。)

Remove extra spaces/lines and re-indent the if/else statements. This worked for me.

(I tried the other solutions here but none worked. My braces were fine.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文