繁体   English   中英

sage 5.0 令人困惑的语法错误

[英]sage 5.0 confusing syntax error

我的操作系统不支持 Sage 5.4,所以我现在只能使用 5.0。 定义此函数不会在 python 中注册任何语法错误,我认为它不会在 Sage 5.4 中出错(如果可能,我希望得到确认。)我想知道为什么它在 5.0 中失败。

def num_matchings(G):
    if min(G.degree_sequence())== 0 or G.num_edges()==0:
        return 0
    elif G.num_edges()==1:
        if G.edges()[0][2] ==None:
            return 1
        else:
            return G.edges()[0][2]
    else:
        H = copy(G)
        K = copy(G)
        e = G.edges()[0]
        if e[2] ==None:
            w=1
        else:
            w = e[2]
        H.delete_edge(e)
        K.delete_vertices([e[0],e[1]])
        return num_matchings(H) + w*num_matchings(K)

我尝试定义时遇到的第一个错误是

File "<ipython console>", line 4 ==Integer(1): ^ SyntaxError: invalid syntax
他们在那之后堆积如山。 在我看来,语法看起来不错。
我使用的是带有 GCC 4.0.1 的 Mac OS 10.5。

非常感谢任何帮助。

[ .delete_vertives().delete_vertives()错字。]

你的语法本身很好。 但是,从错误消息来看,您似乎只是将代码复制并粘贴到控制台中。 这仅适用于某些非常简单的情况。 您还使用制表符进行缩进,这也会导致一系列其他问题。 你真的应该改用 4 空格标签。

如果您想将代码插入实时控制台,您可以使用%paste (如果可以,从剪贴板复制),或%cpaste代替。

例如,如果我复制并粘贴您的代码,我会得到:

sage: def num_matchings(G):
....:         if min(G.degree_sequence())== 0 or G.num_edges()==0:
....:             return 0
....:     elif G.num_edges()==1:
------------------------------------------------------------
   File "<ipython console>", line 4
     ==Integer(1):
      ^
SyntaxError: invalid syntax

sage:         if G.edges()[0][2] ==None:
....:                 return 1
------------------------------------------------------------
   File "<ipython console>", line 2
SyntaxError: 'return' outside function (<ipython console>, line 2)

但是如果我使用%cpaste与 4-space 等效(不幸的是%paste目前不适用于我的 5.4.1 安装):

sage: %cpaste
Pasting code; enter '--' alone on the line to stop.
:
:def num_matchings(G):
:    if min(G.degree_sequence())== 0 or G.num_edges()==0:
:        return 0

[etc.]

:        K.delete_vertices([e[0],e[1]])
:        return num_matchings(H) + w*num_matchings(K)
:--
sage: num_matchings(graphs.LadderGraph(5))
8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM