简体   繁体   English

sage 5.0 令人困惑的语法错误

[英]sage 5.0 confusing syntax error

My OS doesn't support Sage 5.4 so I'm stuck with 5.0 for now.我的操作系统不支持 Sage 5.4,所以我现在只能使用 5.0。 Defining this function registers no syntax errors in python, and I don't think it makes errors in Sage 5.4 (I'd appreciate confirmation if possible.) I'd like to know why it is failing in 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)

The first error I get when I try to define is我尝试定义时遇到的第一个错误是

File "<ipython console>", line 4 ==Integer(1): ^ SyntaxError: invalid syntax
and they pile on after that.他们在那之后堆积如山。 To my eye, the syntax looks fine.在我看来,语法看起来不错。
I'm on Mac OS 10.5 with GCC 4.0.1.我使用的是带有 GCC 4.0.1 的 Mac OS 10.5。

Any help much appreciated.非常感谢任何帮助。

[Aside: typo in .delete_vertives() .] [ .delete_vertives().delete_vertives()错字。]

Your syntax itself is fine.你的语法本身很好。 From the error message, though, it looks like you've simply copied and pasted code into the console.但是,从错误消息来看,您似乎只是将代码复制并粘贴到控制台中。 That will only work in certain very simple cases.这仅适用于某些非常简单的情况。 You're also using tabs for indentation, which can cause a whole other set of headaches too.您还使用制表符进行缩进,这也会导致一系列其他问题。 You should really switch to 4-space tabs instead.你真的应该改用 4 空格标签。

If you want to insert code into a live console, you can use %paste (which copies from the clipboard if it can), or %cpaste instead.如果您想将代码插入实时控制台,您可以使用%paste (如果可以,从剪贴板复制),或%cpaste代替。

For example, if I copy and paste your code, I get:例如,如果我复制并粘贴您的代码,我会得到:

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)

but if I use %cpaste with the 4-space equivalent (unfortunately %paste isn't working on my 5.4.1 install at the moment):但是如果我使用%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