Ins Ins - 1 year ago 76
Python Question

Finding a sum of series in Python 2.7.x

Here's the equation

x + x^2/2 + x^3/3 +.... x^n/n

How will I find the sum of this series? x is a constant term to be entered by user and n which is power is also based on user!

I made this program but it's not working properly.. Have a look -

n=input("Enter power ")
x=input("Enter value of x")
while i<n:
print res

So how do we make it? Thanks a ton for your help!

Answer Source

Something like this for your loop? Tried to keep it simple.

n = input("Enter power ")
x = float(input("Enter value of x"))

ans = 0
for i in range(1, n+1):
    ans += x**i/i


See zev's answer regarding floats

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