Ask Your Question
0

syntax error when doing try-except with the except command

asked 2017-01-04 14:29:30 +0200

livvy94 gravatar image

updated 2017-01-04 17:35:18 +0200

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

Comments

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: https://sagedebianlive.metelu.net/

tmonteil gravatar imagetmonteil ( 2017-01-04 16:10:02 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-01-04 16:07:03 +0200

tmonteil gravatar image

updated 2017-01-04 16:08:33 +0200

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

Comments

@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 17:38:01 +0200 )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 18:17:15 +0200 )edit

Did you add the last missing line ?

tmonteil gravatar imagetmonteil ( 2017-01-04 18:23:48 +0200 )edit

Note 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/357...

tmonteil gravatar imagetmonteil ( 2017-01-04 18:34:34 +0200 )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 17:16:12 +0200 )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

Stats

Asked: 2017-01-04 14:29:30 +0200

Seen: 1,062 times

Last updated: Jan 04 '17