0
Can someone explain the "sum of consecutive numbers" to me? (Python for beginners)
How do I get the code to sum up the list from 0:N+1?
7 Respostas
0
Thank you so much for your quick reply! Unfortunately it doesn't work:
N = int(input())
sum = range(N+1)
s = 0
for i in range(N+1):
s += i
print(s)
Also, I don't really understand it, could you explain it?
0
And why doesn't
N = int(input())
x = sum(range(N+1))
print(x)
do the trick?:)
0
If the sum is from 0 to N+1 included you need to use range(N+2) because the range function does not include the last number
Also, here it's a trick:
(N+1)(N+2)/2
0
The task is the following:
"No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers.
Let’s save some time by creating a program to do the calculation for you!
Take a number N as input and output the sum of all numbers from 1 to N (including N).
Sample Input
100
Sample Output
5050
Explanation: The sum of all numbers from 1 to 100 is equal to 5050."
I wrote N+1 so I can include N. But so far I haven't found the solution to this task. The first line of code is already given
N = int(input())
0
Anyone?
- 1
sum(range(n+1))
- 1
s = 0
for i in range(n+1):
s += i
print(s)