List a range of numbers and add one by one

Asked

Viewed 1,877 times

1

I am with this doubt I do not know how to start, I wonder how could I do to add a number with another of a range for example:

2 = 2
3 = 5
4 = 11

And so on I’m not succeeding, if anyone can help me thank you already.

  • Your question is not clear enough. What do these mean 2 == 2 3 == 5 4 == 11 Are these comparisons or were they supposed to be sums ? What numbers add up to which ? Give a concrete example of input and output so that it is as clear as possible.

  • simple , the program has to print a sequence using the numbers as they are listed for example: if show the 5 the result of the sum will be 5 , now if show the 6 the program has to add the 5 with the 6 resulting in 11 , then it has to add up 5 6 and 7 then results in 18 and so goes

  • In this example of yours the sum ends when ? Does it always begin at 5 ? Is the lower limit of the sum or the upper limit ? What rules are used to establish each of the limits ? These limits are inclusive or exclusive ?

  • In this case the user does not enter any number , the program only runs showing the values

3 answers

2


You can use the function sum the code would be that:

print(sum([1,2,3]))

separated by variables:

lista = [1,2,3]

soma = sum(lista)

print(soma) 

1

If I understand your question, you want to sum up all the elements of a range

use FOR next to the range,

n_elementos = 5
soma = 0

for i in range(1,n_elementos+1):
    soma += i

print(soma)

n_elements+1 is due to range(x,y) from x to y-1

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.