[Tutor] getting error while solving a series that estimates the value of pi

Alan Gauld alan.gauld at btinternet.com
Sun Jul 17 18:59:32 CEST 2011


surya k wrote:
>
> *k = 0*
> *tot = 0*
> *temp1 = 0*
> *while k >= 0 and temp1 == 0 :*
> *      a = ( 2 * math.sqrt(2) ) / 9801 # first term before sigma *
> *      nu = fact (4*k) * (1103 + (26390*k) )  # numerator of series*
> *      de = pow( fact(k), 4 ) * pow ( 396, 4*k )  # denominator of series*
> *      *
> *      if de / nu > 1e-15 : *
> *            temp = nu / de*
> *            tot = tot + temp*
> *            temp1 = 0 *
> *            k = k + 1 *
> *      elif de / nu  == 1e-15 :*
> *             series = a * tot  *

Note that this is the only place you ever define series

> *             k = k + 1*
> *             temp1 = 0*
> *      elif de / nu < 1e-15 :*
> *             print series*

So if you ever get here without de/nu having previously been *exactly* 
1e-15 (which is pretty likely IMHO) you will get a name error.

> *             temp1 = 1*
> 
> 
> I am getting the following error : which is completely surprising!
> 
> *Traceback (most recent call last):*
> *  File "pi.py", line 30, in <module>*
> *    print series*
> *NameError: name 'series' is not defined*

Doesn't surprise me at all...

> Thus I have removed name 'series' and replaced with  *a*tot, *
> now I am getting this error

This is known as "poke 'n hope" debugging.
Better to work out why you got the previous error
message and fix it there. print statements are
your friend

HTH,

Alan G,



More information about the Tutor mailing list