Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 syntax

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

certificate=False): ....: nmax = len(G) + len(G.edges()) # trivial upper bound on the number of colors.

colors. ....: for n in range(1,nmax+1):

range(1,nmax+1): ....: p = MixedIntegerLinearProgram()

MixedIntegerLinearProgram() ....: bv = p.new_variable(binary=True)

p.new_variable(binary=True) ....: be = p.new_variable(binary=True)

p.new_variable(binary=True) ....: for v in G.vertices():

G.vertices(): ....: p.add_constraint(sum(bv[v,c] for c in range(n)) == 1)

1) ....: for e in G.edges(labels=False):

G.edges(labels=False): ....: p.add_constraint(sum(be[e,c] for c in range(n)) == 1)

1) ....: for v in G.vertices():

G.vertices(): ....: for c in range(n):

range(n): ....: p.add_constraint(bv[v,c] + sum(be[e,c] for e in G.edges_incident(v, labels=False)) <= 1)

1) ....: for v,w in G.edges(labels=False):

G.edges(labels=False): ....: for c in range(n):

range(n): ....: p.add_constraint(bv[v,c] + bv[w,c] + be[(v,w),c] <= 1)

1) ....: try:

....: p.solve()

....: p.solve() ....: if certificate:

certificate: ....: bv_sol = p.get_values(bv)

p.get_values(bv) ....: be_sol = p.get_values(be)

p.get_values(be) ....: coloration = {}

{} ....: for v in G.vertices():

G.vertices(): ....: for c in range(n):

range(n): ....: if bv_sol[v,c] == 1:

1: ....: coloration[v] = c

c ....: for e in G.edges(labels=False):

G.edges(labels=False): ....: for c in range(n):

range(n): ....: if be_sol[e,c] == 1:

1: ....: coloration[e] = c

c ....: return coloration

....: else:

coloration ....: else: ....: return n

n ....: except MIPSolverException:

MIPSolverException: File "<ipython-input-17-059a0ac5e5a6>", line 34

34
     except  MIPSolverException:
           ^
SyntaxError: invalid syntax

SyntaxError: invalid syntax

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 syntax