Python Forum

Full Version: summation of series
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to create a function that return the sumer of the following series 1000 +1/1^2 + 1/2^2 ... and so on for the given integer n.

So far I have:

def series_sum():
    n= int (input('Please enter a non-negative integer: '))
    if n<0:
        return None
    else:
        c= 1000
        for i in range(1,n+1):
             c=c+(1/i**2)
             return c
But it returns 1001 instead of 1001.25 when you input n=2

Im not sure what is wrong with my code? Can someone please help me and tell me what is wrong?
if you solve by hand c=c+(1/i**2)for i=1 and c=1000 what do you get? :-)
@buran, You get 1001

But how do I create a function that gives the the sum of the following series 1000 +1/1^2 + 1/2^2+ 1/n^2 ... and so on for the given integer n.
For example, for n = 0, the function should return 1000,
for n = 1, the function should return 1001, for n = 2, the function should return 1001.25, for n = 3, the function should return 1001.3611111111111,
Your return is indented to far. You are always ending the function after the first iteration. Unindent it once.
Thank you :)
(Oct-07-2017, 10:02 PM)student8 Wrote: [ -> ]Thank you :)

Hi, did you find the solution of this question?
Thank you
The solution is in my post (#4).