# [Tutor] Need help w/ a for loop

Monte Milanuk monte at milanuk.net
Fri Oct 24 05:29:33 CEST 2008

```Hello again, and thanks to all of you who extended your help!

Wayne,

Thanks for your examples.  They did end up helping in with finding a pattern to do the odd numbers with.  Maybe I'm not clever enough, but I didn't see how either example could be used for doing the addition/subtraction determination.  Or rather, I found a different way after reading someone else's examples.

Kent,

Thanks for the hint about 'sum', as well as the reminder about float vs. integer math.  Whoops!  As far as whether this is homework or not... not in the traditional sense.  Yes, the book 'Python Programming' is intended as a first-semester comp-sci text, so the questions that I'm working through are probably going to seem an awful lot like homework.  For better or worse, though, the only person assigning 'homework' or grading anything is me.  I am taking some online classes towards an AA degree; sadly, the local community college does not offer (or count) any computer classes online (and with my work schedule, thats the only way that can work for me).  So yes, I do appreciate not being completely spoon-fed as I need to learn this stuff, but no, I'm not 'cheating' on homework for a class per se.

Bob,

Your post was most helpful, in an indirect sort of way.  The examples didn't work directly, or at least they didn't give the right answer (as far as I could tell).  In them was the key (at least, the one I used) to get things working.  The 'x += ...' tidbit was really neat (probably old hat to you) but I couldn't find it in the documentation anywhere - I finally broke down and just started messing with it in the IDLE shell window.  Slick!

Kent, Richard, etc.

While I imagine there are a wide array of ways to do this, especially using more advanced functions or modules... I am trying to work thru this one using the basic tools covered up so far in the book - for loops are about as complex as has been gone into yet.  As such...  I got to beat my head on the screen for a while trying to figure out why some versions did (or didn't) work... including making some very dumb mistakes that kept me working on this for probably a good half-hour to an hour (off and on) longer than I needed to.  Aaarrrgh.

Anyway, here's what I worked out.  Let me know what you think I did right, and what needs tweaked:

IDLE 2.6
>>> (4.0/1)-(4.0/3)+(4.0/5)-(4.0/7)+(4.0/9)
3.3396825396825403
>>> (4.0/1)-(4.0/3)+(4.0/5)-(4.0/7)+(4.0/9)-(4.0/11)
2.9760461760461765
>>> (4.0/1)-(4.0/3)+(4.0/5)-(4.0/7)+(4.0/9)-(4.0/11)+(4.0/13)
3.2837384837384844
>>> (4.0/1)-(4.0/3)+(4.0/5)-(4.0/7)+(4.0/9)-(4.0/11)+(4.0/13)-(4.0/15)
3.0170718170718178
>>>

So using the python shell as a calculator, there are what I work out as the right answers for 5, 6, 7, & 8 iterations.  And here is the program that I worked out that seems to come up with the right answers:

#   approximate_pi.py
#   Approximates the value of 'pi' by summing the terms of a series.
#

import math

def main():
print "This program will approximate the value of pi"
print "to a degree determined by the user. "
print

#  get the value of 'n' from the user
n = input("How many terms do you want me to sum?  ")
print

#  initialize variables
x = 0
m = 1

#  loop thru n times, summing each pass and switching the sign
#  on m each time, and printing x every time through.
for i in range(n):
x = x + m*(4.0/(i*2+1))
m = -m
print x

#  assigning x to pi
pi = x

print
#  output the sum
print "The sum of the numbers you entered is", pi

#  calculate the difference between our approximation and Python's pi
pi_diff = math.pi - pi

#  output the difference (final portion of the problem in the book)
print
print "The difference between your 'pi' & Python's pi is", pi_diff, "."

main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081023/b0c03b79/attachment.htm>
```