Pontius Pontius - 11 months ago 154
Python Question

How can I make this calculate the running total in Python?

I'm trying to create a program that lets me calculate x(i)=1/i^2 for i=1,2,⋯,N

Here is my code so far:

end = int(input("How many times do you want to calculate it?: "))

x = 0.0

for i in range (0, end):
x = x + (1 / end **2)
print ("The sum is", x)


I seem to have a problem with it adding the different values of X together.

How would I do it if i need it to work?

Answer Source

You aren't using your increment i.

You are also dividing by zero.

Try:

end = int(input("How many times do you want to calculate it?: "))

x = 0.0

for i in range (1, end+1):
    x = x + (1 / (i**2))
print ("The sum is", x)

That should provide the result you are looking for. Enjoy!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download