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?

24th Mar 2021, 9:49 PM
Maximilian Hierhammer
7 Antworten
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?
24th Mar 2021, 10:04 PM
Maximilian Hierhammer
0
And why doesn't N = int(input()) x = sum(range(N+1)) print(x) do the trick?:)
24th Mar 2021, 10:09 PM
Maximilian Hierhammer
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
25th Mar 2021, 12:11 AM
Angelo
Angelo - avatar
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())
25th Mar 2021, 6:59 AM
Maximilian Hierhammer
0
Anyone?
29th Mar 2021, 7:09 AM
Maximilian Hierhammer
- 1
sum(range(n+1))
24th Mar 2021, 9:52 PM
Mohamad Kamar
Mohamad Kamar - avatar
- 1
s = 0 for i in range(n+1): s += i print(s)
24th Mar 2021, 9:53 PM
Mohamad Kamar
Mohamad Kamar - avatar