Re: [Tutor] Not returning out the series
Am 04.07.2017 um 04:40 schrieb Rafael Skovron: Hi as a challenge I have got to sum a series i / (i+1). My code isn't summing right. Any ideas why? def main(): print("{0:15s}{1:20s}".format("i","m(i)")) for i in range(1,20): print("{0:<15d}{1:<20.4f}".format(i,m(i))) def m(i): total = 0 for i in range(1,i+1,1): total+=(i/(i+1)) return total Python 2 or Python 3? What sums do you get and what sums do you expect? BTW in your function m(i) it's very confusing that you use the name i both for the range limit and for the loop variable. It seems to be syntactically correct, but it's certainly not readable. HTH Sibylle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not returning out the series
On 04/07/17 03:40, Rafael Skovron wrote: > def m(i): > total = 0 > for i in range(1,i+1,1): > total+=(i/(i+1)) > return total convert the numerator to a float. Otherwise you are using integer division. (I'm guessing you are running Python 2.7?) You could also import division from future: from __future__ import division Alternatively you could try running Python v3. PS, Its probably not a great idea to use i as both the iteration variable and the input parameter. It easily causes confusion. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Not returning out the series
Hi as a challenge I have got to sum a series i / (i+1). My code isn't summing right. Any ideas why? def main(): print("{0:15s}{1:20s}".format("i","m(i)")) for i in range(1,20): print("{0:<15d}{1:<20.4f}".format(i,m(i))) def m(i): total = 0 for i in range(1,i+1,1): total+=(i/(i+1)) return total ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor