PiΒΆ

s = 1
n = -1.0 # set the sign of the next term to be negative
d = 3.0

# calculate the first two terms
s = s + n/d  # take old value then add next term in sequence (-1/3.0)

# set any initial value  for err that is larger than the condition in the while:
err = 10

i = 0  # set the counter i

# start a while loop with a condition based on the absolute error
while abs(err) > 1e-4:
    i = i+1
    n = -1*n
    d = d + 2
    old = s
    s = s + n/d
    err = 4*(s - old)

print(4*s, err, i) # print pi, error and number of steps