[Tutor] Bug in python

Kermit Rose kermit at polaris.net
Sun Feb 19 19:31:01 CET 2006


>>> a = 1
>>> a2 = a%2
>>> a2
1
>>> 8*a2
8
>>> 8*(a%2)
8
>>> 8*a%2
0
>>> 

The * is being given equal  priority to %.

Why isn't % given higher priority than *?


Also,  why am I getting a syntax error  in the following?


The def in the definition of the second function is being highlighted.

IDLE 1.1.2      
>>> def transfac(v):
  a = v[0]
  b = v[1]
  c = v[2]
  d = v[3]
  m = v[4]
  n = v[5]
  z = v[6]
  k = v[7]
  k = k + 1
  a2 = a%2
  b2 = b%2
  c2 = c%2
  d2 = d%2
  ma = ((d - n * c)/(a*m + b))/2
  na = ((d - m * b)/(a * n +c) )/2
  j = 8 * a2 + 4 * b2 + 2 * c2 + d2
  if j == 0:  a,b,c,d,m,n = a/2,b/2,c/2,d/2,m,n
  if j == 1:  a,b,c,d,m,n = -1,-1,-1,-1,-1,-1
  if j == 2:  a,b,c,d,m,n = a,b/2,c,d/2,m,2*n
  if j == 3:  a,b,c,d,m,n = a,(a+b)/2,c,(d-c)/2,2*n+1
  if j == 4:  a,b,c,d,m,n = a,b,c/2,d/2,2*m,n
  if j == 5:  a,b,c,d,m,n = a,b,(a+c)/2,(d-b)/2,2*m+1,n
  if j == 6:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 7:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 8:  a,b,c,d,m,n = -1,-1,-1,-1,-1,-1
  if j == 9:  a,b,c,d,m,n = 2*a,a+b,a+c,(d-a-b-c)/2,2*m+1,2*n+1
  if j == 10:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 11: a,b,c,d,m,n = 2*a,a+b,c,(d-c)/2
  if j == 12:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 13: a,b,c,d,m,n = 2*a,b,a+c,(d-b)/2,m,2*n+1
  if j == 14: a,b,c,d,m,n = 2*a,b,c,d/2,2 *m,2*n 
  if j == 15:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  z = a * d + b * c
  v = [a,b,c,d,m,n,z,k]
  return  v 

 
comment:   the def in the following line has been highlighted.

def gcd(a,b):
  r2 = a
  r3 = b
  flag = 1
  while flag>0:
    if r3 == 0:
      return r2
      flag = 0
    else:
      r1=r2
      r2=r3
      r3 = r1 - r2*(r1/r2)


def factor0(z):
  v = [1,0,0,341,1,1,341,1]
  v[3] = z
  v[6]= z
  flag = 1 
  while flag > 0:
      v = transfac(v)
      g = gcd(z,v[0])
      if g>1:
          if g<z:
              return g
              flag = 0
      g = gcd(z,v[1])
      if g>1:
          if g<z:
              return g
              flag = 0      
      g = gcd(z,v[2])
      if g>1:
          if g<z:
              return g
              flag = 0              
      g = gcd(z,v[3])
      if g>1:
          if g<z:
              return g
              flag = 0
      if v[1]+v[2]+v[3]-v[4]==0:
          return v[0]+v[1]
          flag = 0
      if v[1]+v[2]-v[3]+v[4]==0:
          return v[0]+v[1]
          flag = 0
      if v[1]-v[2]+v[3]+v[4]==0:
          return v[0]+v[2]
          flag = 0
      if -v[1]+v[2]+v[3]+v[4]==0:
          return v[3]+v[1]
          flag = 0


SyntaxError: invalid syntax
>>>  



More information about the Tutor mailing list