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.
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.
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
For this we can use two python functions.
The function crease creating an iterable range of values from the reported parameters and the function sum that adds an eternal and returns the result
follows an example:
sum(range(1,5))
follows the links of the documentation of the functions
Browser other questions tagged python python-3.x
You are not signed in. Login or sign up in order to post.
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.– Isac
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
– Rodolfo Deivis
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 ?
– Isac
In this case the user does not enter any number , the program only runs showing the values
– Rodolfo Deivis