Nicole Walter
Nicole Walter

Reputation: 1

Writing a list that displays 15 approximations of pi

Below I have posted a picture of the task which I want to solve. There I need to show 15 approximations of this number list (here is the equation):

So far I have the following code:

p=3
for i in range(15):
    if (i+1) %2==0:
        p= p-(4/((2+2*i)*(3+2*i)*(4+2*i)))
    else:
        p= p+(4/((2+2*i)*(3+2*i)*(4+2*i)))
        
print(p)

However it just gives me the 15th result. Can someone please help me?

Upvotes: 0

Views: 364

Answers (1)

Ralubrusto
Ralubrusto

Reputation: 1501

You are using a variable that is uptaded at every iteration. You need to store the desired values in a list or something, like:

p=3
my_pis = []
for i in range(15):
    if (i+1) %2==0:
        p= p-(4/((2+2*i)*(3+2*i)*(4+2*i)))
    else:
        p= p+(4/((2+2*i)*(3+2*i)*(4+2*i)))
    my_pis.append(p)

        
print(my_pis)

Upvotes: 1

Related Questions