Ask Your Question

syntax error when doing try-except with the except command

asked 2017-01-04 07:29:30 -0600

livvy94 gravatar image

updated 2017-01-04 10:35:18 -0600

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 syntax
edit retag flag offensive close merge delete


If 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:

tmonteil gravatar imagetmonteil ( 2017-01-04 09:10:02 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-04 09:07:03 -0600

tmonteil gravatar image

updated 2017-01-04 09:08:33 -0600

I 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:
    ....:             pass
edit flag offensive delete link more


@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?

livvy94 gravatar imagelivvy94 ( 2017-01-04 10:38:01 -0600 )edit

sage 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?

nbruin gravatar imagenbruin ( 2017-01-04 11:17:15 -0600 )edit

Did you add the last missing line ?

tmonteil gravatar imagetmonteil ( 2017-01-04 11:23:48 -0600 )edit

Note that the complete code can be found here and works without problem (at least copy-pasted from the command line)

tmonteil gravatar imagetmonteil ( 2017-01-04 11:34:34 -0600 )edit

@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?

livvy94 gravatar imagelivvy94 ( 2017-01-07 10:16:12 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-01-04 07:29:30 -0600

Seen: 111 times

Last updated: Jan 04