Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
summation of series
#1
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?
Reply
#2
if you solve by hand c=c+(1/i**2)for i=1 and c=1000 what do you get? :-)
Reply
#3
@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,
Reply
#4
Your return is indented to far. You are always ending the function after the first iteration. Unindent it once.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thank you :)
Reply
#6
(Oct-07-2017, 10:02 PM)student8 Wrote: Thank you :)

Hi, did you find the solution of this question?
Thank you
Reply
#7
The solution is in my post (#4).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding a summation formula rafl0913 3 2,957 Mar-01-2020, 07:37 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020