Accessing variable from a function within a function
James Stroud
jstroud at mbi.ucla.edu
Sun Jun 24 21:41:45 CEST 2007
James Stroud wrote:
> Nathan Harmston wrote:
> def exteuclid(m,n):
> x = 0,1,1,0,m,n
> def euclid(c,d,x=x):
> a,a1,b,b1,c,d = x
> q = c /d
> r = c % d
> if r == 0:
> print a,b
> return d
> else:
> print a1,a,b1,b,c,d,q,r
> t = b1
> b = t - q * b
> a = t - q * a
> c,d,a1,b1 = d,r,a,b
> return euclid(c,d)
> return euclid(c,d)
>
> James
My answer above is wrong because c and d take the wrong default values.
Also, you have some ambiguity in your code. Are nested calls to euclid
supposed to have the original values of a, a1, b, & b1, or are they to
take the original values 0, 1, 1, 0? This type of ambiguity is one
reason why the interpreter does not like reference before assignment.
This is my best guess at what you want because I'm not familiar with how
euclid's algorithm works:
def exteuclid(m,n):
def euclid(a,a1,b,b1,c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t - q * b
a = t - q * a
c,d,a1,b1 = d,r,a,b
return euclid(a,a1,b,b1,c,d)
return euclid(0,1,1,0,m,n)
More information about the Python-list
mailing list