[Tutor] A better way to estimate the value of Pi?

Sagar Shankar sagar at scubed.in
Mon Oct 17 12:31:24 CEST 2011

Hi, this is my first question to this group. I'm a beginner to computer
science and programming in Python. Am currently using John Zelle's book -
Python Programming: An introduction to computer science to teach myself.

In the book, there is an exercise to create a program that approximates the
value of Pi by using the series [image: image.png]

Now, the only way I've been able to figure out how to do this is by creating
pairs of 4/x - 4/x+2 and calculating the series. The problem is that the
exercise asks to prompt the user for the number of terms to sum, and then
output the sum. So if an user inputs just 1, then his answer should be 4/1,
whereas in my version it will 4/1-4/3

Is there anyway I can implement this without using my current pair method? I
don't want to us if-else statements as it has not been introduced at this
level of the book and I really want to know if there is an algorithmic way
I'm missing.

My program is copied below for your reference. Thanks a lot for your help!


import math

def main():

counter = 0.0
y = 1.0
 seed = input("Please enter the number of pairs to calculate: ")
for i in range(1, seed+1):
 z = y + 2
approx = 4.0/(y) - 4.0/(z)
counter = counter + approx
 y = z + 2
print "The difference between computed value of Pi and actual value is ",

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111017/5daa0c4e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 1843 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20111017/5daa0c4e/attachment.png>

More information about the Tutor mailing list