on the way to find pi!

Fredrik Lundh fredrik at pythonware.com
Sun Jan 23 14:10:27 EST 2005

Ali Polatel wrote:

> write the code type str(pi(5)) and see what I mean

it helps if you post the code you want help with in your first post,
so people don't have to guess.

the pi function doesn't return anything, it prints the value to stdout
(that's what the stdout.write things are doing).

if a function doesn't return anything, Python automagically inserts
a "return None", which explains the "None" you're seeing (well, it
prints 3.1415None after your modification, so you probably did
something else to get "3.1415'None'").

if you want the digits, you have to change the stdout.write calls to
something else.  say:

def pi(n=-1):
    output = ""
    printed_decimal = False
    r = f((1,0,1,1))
    while n != 0:
        if len(r) == 5:
            output += str(r[4])
            if not printed_decimal:
                output += '.'
                printed_decimal = True
            n -= 1
        r = f(r[:4])
    return output

(this returns a string.  to convert it to a Python float, use float(pi(x)).  if x is
large enough, that gives you the same thing as math.pi).


More information about the Python-list mailing list