+ 6
How do I do the sum of consecutive numbers?
I can't manage it and I just started learning python yesterday. Edit: I managed
19 Respuestas
+ 10
There are several ways to do this.
One way includes these concepts:
loops
range
See if you can figure it out now.
+ 9
Sami Neumann ,
if you really have a problem, you should make a little more effort and we need some more details from you:
▪︎give a clear and complete description about your task
▪︎if your question is related to an exercise in a tutorial, please mention the tutorial name and the lesson number
▪︎if there are error messages please post them here
▪︎give at least one sample with input data and the expected output
▪︎to be able to find out the issues you have, we need to see your code
=> please put your code in playground, save it there and post a link to it here.
thanks for your understanding!
+ 8
praneetha [Left due to Exams📓] ,
the formula is ok to know, but in this case it is more helpful to learn how to use the basics of python. learning of loops, ranges and building up a problem solving ability is essential for the success of a coder.
+ 7
Sami Neumann ,
since you have already learned about ranges and for loops, i would recommend you to use this:
▪︎create an input for the upper bound number, convert it to int and store it in a variable
▪︎create a variable like total and initialize it with 0
▪︎use a for loop and a range (as iterable) using the upper bound number (+1) and iterate over the range
▪︎the loop variable will now get one number in each iteration cycle
▪︎add the number in the loop variable to the variable total
▪︎when iteration is done, the required result is in variable total
+ 7
Runtime Terror ,
2 issues:
(1) the code you presented does not output the correct result. sum of numbers up to 100 should give 5050. to get results correctly, please keep in mind that using range does not include the upper bound. so we have to add +1 (means up to 101):
...
for i in range(start, (start + (end - start)+1)):
...
(2) it does not help the asker if he will get a ready-made code. it is more helpful to give hints or tips.
thanks for your understanding!
+ 4
Any example ? Or question asks this much only ?
+ 2
This is what I have to do.
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
Its from 36 code project in python for begginers. I just can't figure it out. Lothar Abhay Simon Sauter
+ 2
Gauss was a genious when found that formula praneetha [Left due to Exams📓] Divyanshu Yadav
It transforms an algorithm with complexity of O(n) linear
S = sum(n for n in range(a, b))
into constant O(1)
S = n • (n - 1) / 2
And can resolve the sum of trillions of numbers in a shot of time. With linear approach cost a bit more, even using concurrent programming dividing work in chunk tasks.
+ 1
You can simply use this formula : N(N+1)/2
+ 1
You can use range function like this
s=eval(input("enter your number"))
s1=eval(input("enter your 2nd number"))
For i in range(s,s1,s+1):
Print(i)
0
Very much forms
0
Or you can use the formula n(n+1)/2 for the input n.
0
You could do this in one loop, by using range to define your numbers, and sum to loop through the numbers for you.
>>> x = input("Please input an integer: ") Please input an integer: 5 >>> x = int(x) >>> >>> for i in range(1, x+1): ... nums = range(1, i+1) ... print(' + '.join(map(str, nums)), '=', sum(nums))