Re: [Tutor] import of source code still not working
>>If you don't want to have to put the factor30 in front of all your >>function >> names you can do this: >> >> from factor30 import * But this is generally considered bad practice since the same function name can appear in many modules so the last module imported will hide all the others. The small increase in typing is a small price to pay for much more reliable code. You can control visibility by doing from factor30 import factor, someOtherFunc, andAnother ie list the names you need to use explicitly. That way you can see potential name clashes more easily. > Next dilemma is: >>> v = [2, 35715, 17859, -318417088, 8932, 17860, 109, 5] >>> v = transfac(v) -1 2 35715 17859 -318417088 8932 17860 Traceback (most recent call last): File "", line 1, in -toplevel- v = transfac(v) File "c:\math\factoring\factor30.py", line 46, in transfac a,b,c,d,m,n = a,b.c+a*t,d-b*t,m+t,n AttributeError: 'int' object has no attribute 'c' # Read the error message carefully then look at the code. int object with nom attribute c. where does c appear? You have a dot between b and c which I assume should be a comma. Dot notation is how you access attributes of an object - like the functions in a module for example. Sop Python is looki8ng for the c attribute of b but b is a number... HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] import of source code still not working
On 20/02/06, Kermit Rose <[EMAIL PROTECTED]> wrote: From: Jason Massey Date: 02/20/06 12:20:03 To: Kermit Rose Cc: tutor@python.org Subject: Re: [Tutor] import of source code still not working If you don't want to have to put the factor30 in front of all your function names you can do this:from factor30 import *Which will put all of your functions into the global namespace.Then you can call factor() & factor0() as you would expect to. *** Thank you very much Next dilemma is: >>> v = [2, 35715, 17859, -318417088, 8932, 17860, 109, 5]>>> v = transfac(v)-12 35715 17859 -318417088 8932 17860 Traceback (most recent call last): File "", line 1, in -toplevel- v = transfac(v) File "c:\math\factoring\factor30.py", line 46, in transfac a,b,c,d,m,n = a,b.c+a*t,d-b*t,m+t,nAttributeError: 'int' object has no attribute 'c'>>> v[2, 35715, 17859, -318417088, 8932, 17860, 109, 5] Listing of transfac function is def transfac(v):# import random# begin level 0 a = v[0] b = v[1] c = v[2] d = v[3] m = v[4] n = v[5] z = v[6] # line 10 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))/4# ma = random.randint(1,min(abs(ma),abs(m))+1)# na = ((d - m * b)/(a * n +c) )/4# na = random.randint(1,min(abs(na),abs(n))+1) # line 20# if v[3]<0:# na=-na# ma=-ma# ma = ma * random.randint(0,1)# if ma=0:# pass# else# na = na * random.randint(0,1) 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 # line 30 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: # begin if level 1 if b>c: # begin if level 2 t=(d-b*m)/(a+c*m) # line 40 t=t/4 print t if t==0: t=1 print a,b,c,d,m,n a,b,c,d,m,n = a,b.c+a*t,d-b*t,m+t,n # end if level 2 # line 46 else: # begin if level 2 t=(d-c*n)/(b+a*m) t=t/4 # line 50 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1# 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: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a*m) t=t/4 if t == 0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n else: # continue if level 2 t=(d - c * n)/(b+a*n) t=t/4 if t == 0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 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: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a * m) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n # end if level 2 else: # continue if level 1 t=(d - c * n)/(c + a * n) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 if j == 11: a,b,c,d,m,n = 2*a,a+b,c,(d-c)/2 if j == 12: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a * m) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n else: # continue if level 2 t=(d - c * n)/(b + a * n) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 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: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a * m) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n else: # continue if level 2 t=(d - c * n)/(b + a * n) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 z = a * d + b * c v = [a,b,c,d,m,n,z,k] return v# end routine level Kermit have you actually bothered going through any tutorials? You just seem to be as
Re: [Tutor] import of source code still not working
From: Jason Massey Date: 02/20/06 12:20:03 To: Kermit Rose Cc: tutor@python.org Subject: Re: [Tutor] import of source code still not working If you don't want to have to put the factor30 in front of all your function names you can do this:from factor30 import *Which will put all of your functions into the global namespace.Then you can call factor() & factor0() as you would expect to. *** Thank you very much Next dilemma is: >>> v = [2, 35715, 17859, -318417088, 8932, 17860, 109, 5]>>> v = transfac(v)-12 35715 17859 -318417088 8932 17860 Traceback (most recent call last): File "", line 1, in -toplevel- v = transfac(v) File "c:\math\factoring\factor30.py", line 46, in transfac a,b,c,d,m,n = a,b.c+a*t,d-b*t,m+t,nAttributeError: 'int' object has no attribute 'c'>>> v[2, 35715, 17859, -318417088, 8932, 17860, 109, 5] Listing of transfac function is def transfac(v):# import random# begin level 0 a = v[0] b = v[1] c = v[2] d = v[3] m = v[4] n = v[5] z = v[6] # line 10 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))/4# ma = random.randint(1,min(abs(ma),abs(m))+1)# na = ((d - m * b)/(a * n +c) )/4# na = random.randint(1,min(abs(na),abs(n))+1) # line 20# if v[3]<0:# na=-na# ma=-ma# ma = ma * random.randint(0,1)# if ma=0:# pass# else# na = na * random.randint(0,1) 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 # line 30 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: # begin if level 1 if b>c: # begin if level 2 t=(d-b*m)/(a+c*m) # line 40 t=t/4 print t if t==0: t=1 print a,b,c,d,m,n a,b,c,d,m,n = a,b.c+a*t,d-b*t,m+t,n # end if level 2 # line 46 else: # begin if level 2 t=(d-c*n)/(b+a*m) t=t/4 # line 50 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1# 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: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a*m) t=t/4 if t == 0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n else: # continue if level 2 t=(d - c * n)/(b+a*n) t=t/4 if t == 0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 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: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a * m) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n # end if level 2 else: # continue if level 1 t=(d - c * n)/(c + a * n) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 if j == 11: a,b,c,d,m,n = 2*a,a+b,c,(d-c)/2 if j == 12: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a * m) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n else: # continue if level 2 t=(d - c * n)/(b + a * n) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 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: # begin if level 1 if b>c: # begin if level 2 t=(d - b * m)/(c + a * m) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b,c+a*t,d-b*t,m+t,n else: # continue if level 2 t=(d - c * n)/(b + a * n) t=t/4 if t==0: t=1 a,b,c,d,m,n = a,b+a*t,c,d-c*t,m,n+t # end if level 2 # end if level 1 z = a * d + b * c v = [a,b,c,d,m,n,z,k] return v# end routine level 0 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] import of source code still not working
The problem here is that you need to reference the factor30 namespace to get to your functions: Your calls to your functions would then look like: factor30.factor(109) & factor30.factor0(109) If you don't want to have to put the factor30 in front of all your function names you can do this: from factor30 import * Which will put all of your functions into the global namespace. Then you can call factor() & factor0() as you would expect to. On 2/20/06, Kermit Rose <[EMAIL PROTECTED]> wrote: IDLE 1.1.2>>> import factor30>>> factor(109)Traceback (most recent call last): File "", line 1, in -toplevel-factor(109)NameError: name 'factor' is not defined >>> factor0(109)Traceback (most recent call last): File "", line 1, in -toplevel-factor0(109)NameError: name 'factor0' is not defined>>> ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] import of source code still not working
IDLE 1.1.2 >>> import factor30 >>> factor(109) Traceback (most recent call last): File "", line 1, in -toplevel- factor(109) NameError: name 'factor' is not defined >>> factor0(109) Traceback (most recent call last): File "", line 1, in -toplevel- factor0(109) NameError: name 'factor0' is not defined >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor