[Tutor] Help with Recurring Function

Sanhita Mallick sanhitam at yahoo.com
Sat Apr 26 14:16:34 CEST 2008


Hi Kent.

sum_norm_ted is sum over each i, and all j. That is
why it is initialize to zero at the beginning of each
i loop.

The inner loop IS running - which I checked by all the
print statements. The value of "ted" and "mag" ARE
being calculated correctly as checked by the "print"
statement. But when we do 

norm_ted = (2 * ted) / mag

becomes zero, and subsequently sum_norm_ted becomes
zero. 


--- Kent Johnson <kent37 at tds.net> wrote:

> Sanhita Mallick wrote:
> 
> > I am struggling with a simple recurring function
> 
> What is a recurring function? Do you mean looping?
> > 
> > Here is the script. T(i) are trees (as in graphs).
> > The program operates great, until the step where
> > norm_ted is calculated. Mysteriously norm_ted
> becomes
> > 0, even though "mag" value is calculated
> correctly.
> > Also, strangely, sum_norm_ted also becomes zero. 
> > 
> > What am I missing?
> 
> Do you mean for sum_norm_ted to be the sum over all
> i and j? If so, you 
> should initialize it before the start of the outer
> loop.
> 
> I don't know why you are getting 0 at the end of the
> loop. It is as if 
> sum_norm_ted and norm_ted are initialized but the
> inner loop doesn't 
> run. Is this the exact code that you have trouble
> with?
> 
> Kent
> > 
> > Thanks.
> > -Sanhita
> > 
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > T = [F1, F2, F3]
> > k = len(T)
> > mean_ted = {}
> > for i in range(k):    
> >     sum_norm_ted = 0.0
> >     mag = 0.0
> >     norm_ted = 0.0
> >     for j in range(k):
> >         if (i != j):
> >             ted= alg.ted(T[i], T[j])
> >             print 'TED ' + str(i) + str(j) + ' is
> ' +
> > str(ted)
> >             mag = 2 * (T[i].n + T[j].n) - 1
> >             print 'MAG ' + str(i) + str(j) + ' is
> ' +
> > str (mag) + ' and ' + str ( 2 * ted )
> >             norm_ted = (2 * ted) / mag 
> >             print 'norm_ted ' + str(i) + str(j) +
> ' is
> > ' + str (norm_ted)
> >             sum_norm_ted = sum_norm_ted + norm_ted
> >             print 'sum_norm_ted ' + str(i) +
> str(j) +
> > ' is ' + str (sum_norm_ted)
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> 
> 



More information about the Tutor mailing list