ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 07 Jan 2017 18:03:34 +0100syntax error when doing try-except with the except commandhttps://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/I am running sage via virtual box and for some reason I am getting a syntax error for except. I was wondering if this wasn't available because I am using virtual box or if I am doing something wrong.
example below,
sage: def total_chromatic_number(G, certificate=False):
....: nmax = len(G) + len(G.edges()) # trivial upper bound on the number of colors.
....: for n in range(1,nmax+1):
....: p = MixedIntegerLinearProgram()
....: bv = p.new_variable(binary=True)
....: be = p.new_variable(binary=True)
....: for v in G.vertices():
....: p.add_constraint(sum(bv[v,c] for c in range(n)) == 1)
....: for e in G.edges(labels=False):
....: p.add_constraint(sum(be[e,c] for c in range(n)) == 1)
....: for v in G.vertices():
....: for c in range(n):
....: p.add_constraint(bv[v,c] + sum(be[e,c] for e in G.edges_incident(v, labels=False)) <= 1)
....: for v,w in G.edges(labels=False):
....: for c in range(n):
....: p.add_constraint(bv[v,c] + bv[w,c] + be[(v,w),c] <= 1)
....: try:
....: p.solve()
....: if certificate:
....: bv_sol = p.get_values(bv)
....: be_sol = p.get_values(be)
....: coloration = {}
....: for v in G.vertices():
....: for c in range(n):
....: if bv_sol[v,c] == 1:
....: coloration[v] = c
....: for e in G.edges(labels=False):
....: for c in range(n):
....: if be_sol[e,c] == 1:
....: coloration[e] = c
....: return coloration
....: else:
....: return n
....: except MIPSolverException:
File "<ipython-input-17-059a0ac5e5a6>", line 34
except MIPSolverException:
^
SyntaxError: invalid syntaxWed, 04 Jan 2017 14:29:30 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/Comment by tmonteil for <p>I am running sage via virtual box and for some reason I am getting a syntax error for except. I was wondering if this wasn't available because I am using virtual box or if I am doing something wrong. </p>
<p>example below,</p>
<pre><code>sage: def total_chromatic_number(G, certificate=False):
....: nmax = len(G) + len(G.edges()) # trivial upper bound on the number of colors.
....: for n in range(1,nmax+1):
....: p = MixedIntegerLinearProgram()
....: bv = p.new_variable(binary=True)
....: be = p.new_variable(binary=True)
....: for v in G.vertices():
....: p.add_constraint(sum(bv[v,c] for c in range(n)) == 1)
....: for e in G.edges(labels=False):
....: p.add_constraint(sum(be[e,c] for c in range(n)) == 1)
....: for v in G.vertices():
....: for c in range(n):
....: p.add_constraint(bv[v,c] + sum(be[e,c] for e in G.edges_incident(v, labels=False)) <= 1)
....: for v,w in G.edges(labels=False):
....: for c in range(n):
....: p.add_constraint(bv[v,c] + bv[w,c] + be[(v,w),c] <= 1)
....: try:
....: p.solve()
....: if certificate:
....: bv_sol = p.get_values(bv)
....: be_sol = p.get_values(be)
....: coloration = {}
....: for v in G.vertices():
....: for c in range(n):
....: if bv_sol[v,c] == 1:
....: coloration[v] = c
....: for e in G.edges(labels=False):
....: for c in range(n):
....: if be_sol[e,c] == 1:
....: coloration[e] = c
....: return coloration
....: else:
....: return n
....: except MIPSolverException:
File "<ipython-input-17-059a0ac5e5a6>", line 34
except MIPSolverException:
^
SyntaxError: invalid syntax
</code></pre>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36174#post-id-36174If you are not sure about Virtualbox, and want to try Sage without installing GNU/Linux first (which i recommend anyway), you can run it from a live USB: https://sagedebianlive.metelu.net/Wed, 04 Jan 2017 16:10:02 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36174#post-id-36174Answer by tmonteil for <p>I am running sage via virtual box and for some reason I am getting a syntax error for except. I was wondering if this wasn't available because I am using virtual box or if I am doing something wrong. </p>
<p>example below,</p>
<pre><code>sage: def total_chromatic_number(G, certificate=False):
....: nmax = len(G) + len(G.edges()) # trivial upper bound on the number of colors.
....: for n in range(1,nmax+1):
....: p = MixedIntegerLinearProgram()
....: bv = p.new_variable(binary=True)
....: be = p.new_variable(binary=True)
....: for v in G.vertices():
....: p.add_constraint(sum(bv[v,c] for c in range(n)) == 1)
....: for e in G.edges(labels=False):
....: p.add_constraint(sum(be[e,c] for c in range(n)) == 1)
....: for v in G.vertices():
....: for c in range(n):
....: p.add_constraint(bv[v,c] + sum(be[e,c] for e in G.edges_incident(v, labels=False)) <= 1)
....: for v,w in G.edges(labels=False):
....: for c in range(n):
....: p.add_constraint(bv[v,c] + bv[w,c] + be[(v,w),c] <= 1)
....: try:
....: p.solve()
....: if certificate:
....: bv_sol = p.get_values(bv)
....: be_sol = p.get_values(be)
....: coloration = {}
....: for v in G.vertices():
....: for c in range(n):
....: if bv_sol[v,c] == 1:
....: coloration[v] = c
....: for e in G.edges(labels=False):
....: for c in range(n):
....: if be_sol[e,c] == 1:
....: coloration[e] = c
....: return coloration
....: else:
....: return n
....: except MIPSolverException:
File "<ipython-input-17-059a0ac5e5a6>", line 34
except MIPSolverException:
^
SyntaxError: invalid syntax
</code></pre>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?answer=36173#post-id-36173I see two possible problems with your code:
- the `MIPSolverException` is not imported by Sage by default, so you have to import it first:
sage: from sage.numerical.mip import MIPSolverException
- when you write
....: except MIPSolverException:
you have to do something with it, like:
....: except MIPSolverException:
....: print 'n is not large enough
or, more simply
....: except MIPSolverException:
....: passWed, 04 Jan 2017 16:07:03 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?answer=36173#post-id-36173Comment by livvy94 for <p>I see two possible problems with your code:</p>
<ul>
<li><p>the <code>MIPSolverException</code> is not imported by Sage by default, so you have to import it first:</p>
<pre><code> sage: from sage.numerical.mip import MIPSolverException
</code></pre></li>
<li><p>when you write</p>
<pre><code>....: except MIPSolverException:
</code></pre>
<p>you have to do something with it, like:</p>
<pre><code>....: except MIPSolverException:
....: print 'n is not large enough
</code></pre>
<p>or, more simply</p>
<pre><code>....: except MIPSolverException:
....: pass
</code></pre></li>
</ul>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36177#post-id-36177@tmonteil Thank you. I have now imported the MIPSolverException and tried again but I still appear to be getting the syntax error on except so am unable to put in print or pass?Wed, 04 Jan 2017 17:38:01 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36177#post-id-36177Comment by nbruin for <p>I see two possible problems with your code:</p>
<ul>
<li><p>the <code>MIPSolverException</code> is not imported by Sage by default, so you have to import it first:</p>
<pre><code> sage: from sage.numerical.mip import MIPSolverException
</code></pre></li>
<li><p>when you write</p>
<pre><code>....: except MIPSolverException:
</code></pre>
<p>you have to do something with it, like:</p>
<pre><code>....: except MIPSolverException:
....: print 'n is not large enough
</code></pre>
<p>or, more simply</p>
<pre><code>....: except MIPSolverException:
....: pass
</code></pre></li>
</ul>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36178#post-id-36178sage parses your code without complaining if I paste it into 7.5beta . It complains about indentation if I do it in 6.7, and advises to use %cpaste. Perhaps you have a tab/strange character somewhere that got converted when you pasted the code into your question?Wed, 04 Jan 2017 18:17:15 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36178#post-id-36178Comment by tmonteil for <p>I see two possible problems with your code:</p>
<ul>
<li><p>the <code>MIPSolverException</code> is not imported by Sage by default, so you have to import it first:</p>
<pre><code> sage: from sage.numerical.mip import MIPSolverException
</code></pre></li>
<li><p>when you write</p>
<pre><code>....: except MIPSolverException:
</code></pre>
<p>you have to do something with it, like:</p>
<pre><code>....: except MIPSolverException:
....: print 'n is not large enough
</code></pre>
<p>or, more simply</p>
<pre><code>....: except MIPSolverException:
....: pass
</code></pre></li>
</ul>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36179#post-id-36179Did you add the last missing line ?Wed, 04 Jan 2017 18:23:48 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36179#post-id-36179Comment by tmonteil for <p>I see two possible problems with your code:</p>
<ul>
<li><p>the <code>MIPSolverException</code> is not imported by Sage by default, so you have to import it first:</p>
<pre><code> sage: from sage.numerical.mip import MIPSolverException
</code></pre></li>
<li><p>when you write</p>
<pre><code>....: except MIPSolverException:
</code></pre>
<p>you have to do something with it, like:</p>
<pre><code>....: except MIPSolverException:
....: print 'n is not large enough
</code></pre>
<p>or, more simply</p>
<pre><code>....: except MIPSolverException:
....: pass
</code></pre></li>
</ul>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36180#post-id-36180Note that the complete code can be found here and works without problem (at least copy-pasted from the command line) https://ask.sagemath.org/question/35744/can-you-find-the-total-chromatic-number-edge-and-vertices-of-a-graph/Wed, 04 Jan 2017 18:34:34 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36180#post-id-36180Comment by livvy94 for <p>I see two possible problems with your code:</p>
<ul>
<li><p>the <code>MIPSolverException</code> is not imported by Sage by default, so you have to import it first:</p>
<pre><code> sage: from sage.numerical.mip import MIPSolverException
</code></pre></li>
<li><p>when you write</p>
<pre><code>....: except MIPSolverException:
</code></pre>
<p>you have to do something with it, like:</p>
<pre><code>....: except MIPSolverException:
....: print 'n is not large enough
</code></pre>
<p>or, more simply</p>
<pre><code>....: except MIPSolverException:
....: pass
</code></pre></li>
</ul>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36216#post-id-36216@tmonteil @nbruin thank you :) looks like it was a indentation error as now it works. However once I enter pass and then press enter I end up with ....: not sage: how do I get out of this?Sat, 07 Jan 2017 17:16:12 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36216#post-id-36216Comment by nbruin for <p>I see two possible problems with your code:</p>
<ul>
<li><p>the <code>MIPSolverException</code> is not imported by Sage by default, so you have to import it first:</p>
<pre><code> sage: from sage.numerical.mip import MIPSolverException
</code></pre></li>
<li><p>when you write</p>
<pre><code>....: except MIPSolverException:
</code></pre>
<p>you have to do something with it, like:</p>
<pre><code>....: except MIPSolverException:
....: print 'n is not large enough
</code></pre>
<p>or, more simply</p>
<pre><code>....: except MIPSolverException:
....: pass
</code></pre></li>
</ul>
https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36217#post-id-36217Have you tried entering an empty line? I think this is just the prompt allowing you to enter multi-line commands and you have to indicate somehow that you're done.
An alternative is to use "%cpaste" and indicate the end of input with "--". That works better because it allows you to have empty lines in your input.
Once you have a lot of multi-line input, you should probably move to the notebook interface or put your routines in a file that you then load or import.Sat, 07 Jan 2017 18:03:34 +0100https://ask.sagemath.org/question/36168/syntax-error-when-doing-try-except-with-the-except-command/?comment=36217#post-id-36217