Search code examples
pythonsum

Python, how I can find sum of this elements?


Find the sum of n elements of the following row of numbers:
1, -0.5, 0.25, -0.125 ...
The number of elements (n) is entered from the keyboard.


Solution

  • You're looking for something like this?

    l = []
    for i in range(int(input())):
        if i%2 ==0:
            l.append(1/2**i)
        else:
            l.append(-(1/2**i))
    print(sum(l))