-1

Image of question

I am trying to use a for loop to calculate the sum as the total number of values in the series increase by the power of 10. My code so far does not calculate anything. I apologise. I'm new to Python and still learning. Any help is much appreciated! Thank you!

This is my code:

i = 1
sum = 100

for num in range((-1)^(i+1)):
    sum += num
print("The sum of the first ", i, "numbers is: ", sum )

EDIT Hello, thank you for all your help! But it seems like for my problem, I need to individually print out all three of the summations using the for loop. Could I perhaps ask for help on this part? Thank you so much!

Yue Shang
  • 13
  • 2

3 Answers3

2
sum = 0
for i in range(1, 100+1):
    sum += ((-1)**(i+1))/i
print(sum)
  • I am glad this snipped helped, read more about [range](https://stackoverflow.com/questions/4504662/why-does-rangestart-end-not-include-end) here – Srinadha Reddy Aug 16 '20 at 07:41
1

Maybe you can start with this (the first one): First create a dummy variable which will collect your result - here I call sum. Next just loop over the required number of times.
(Note that the power in python is ** and not ^)

    sum =0
    for i in range(1,100):
       sum+=((-1)**(i+1))/i
1

Here is an alternative way using Lists (one line of code :) )

print(sum([(-1)**(i+1) for i in range(1,100)]))