Deoxyribonucleic Acid - 1 year ago 136
Python Question

# generating pi in python

I would like to know how to adapt this code to give me X digits of pi when asked
because right now it just prints a random amount

``````while True:
print("how many digits of pi would you like")

def make_pi():
q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
number = int(input())
for j in range(int(number)):
if 4 * q + r - t < m * t:
yield m
q, r, t, k, m, x = 10 * q, 10 * (r - m * t), t, k, (10 * (3 * q + r)) // t - 10 * m, x
else:
q, r, t, k, m, x = q * k, (2 * q + r) * x, t * x, k + 1, (q * (7 * k + 2) + r * x) // (t * x), x + 2

digits = make_pi()
pi_list = []
my_array = []

for i in make_pi():
my_array.append(str(i))

my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array)
print("here is the string:\n %s" % big_string)
``````

``````print('{:.xf}'.format(math.pi))
``````

Where `x` is the number of digits of pi you want to print after the decimal.

EDIT:

IF you want to do it you way then

``````big_string = ''.join(my_array[:x])
``````

Where x is the number of characters including leading 3 and decimal point.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download