0

Sum of consecutive numbers

N = int(input()) sum = 0 for x in range(1, 100): x+=1 print(x) Hi All, I don’t know where I’m going wrong, can somebody help?

9th Jul 2022, 10:17 AM
Reiss
35 Answers
+ 8
start = int(input) end = int(input) sum = 0 for i in range(start, end) sum += i print(sum) This would print the sum of all numbers between the start and the end
9th Jul 2022, 10:25 AM
Janne
Janne - avatar
+ 8
n = int(input()) x = 0 # important declaration.. for i in range(1,n+1): x += i print(x)
10th Jul 2022, 6:34 AM
Rajeev Sharma
Rajeev Sharma - avatar
+ 6
The for loop should look like this...... for x in range(1, N + 1):
9th Jul 2022, 10:34 AM
Quantum
Quantum - avatar
+ 5
N = int(input()) sum = 0 for x in range(1, N): sum=sum+x print(sum) -> you need to add every value of x to sum ->then print the sum...
9th Jul 2022, 10:23 AM
Mihir Lalwani
Mihir Lalwani - avatar
+ 5
we are using here a variable named 'sum'. but there is a builtin function that has the same name. this leads to a crash of the code, when this builtin function is used in the same code as the variable 'sum'. run the code snippet to see what happens: https://code.sololearn.com/cQ795u5aTh4g/?ref=app the same can happen when using these variable names: list, max, min, dict, ... and more
10th Jul 2022, 7:44 PM
Lothar
Lothar - avatar
+ 5
The answer to your problem using three different methods: https://code.sololearn.com/crKOSyW75BLv/?ref=app
10th Jul 2022, 8:24 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
+ 5
N = int(input()) sum = 0 for x in range(1, N+1): sum+=x print(sum) You need to add every value of x to sum. Then print the sum of all number. This would print the sum of all numbers between the start and the end. You must not add the variable x as sum since it is the index value you need to add them for addition to get the correct value of the sum of consecutive numbers.
11th Jul 2022, 5:01 AM
MASTER JAVA
MASTER JAVA - avatar
+ 3
You can’t add x as sum since it is the index value you need to add them for sum to get the correct value
9th Jul 2022, 2:17 PM
Ahmadullah Naibi
+ 2
your code is actually printing how many times the loop runs, if you want to sum two consecutive numbers you can use this code: num1 = int(input("first number")) num2 = int(input("second number")) print(num1+num2) or if you want to add, substract, or divide two or more numbers, you can use this code: print(eval(int(input()))) for thacñt code you need an input like "13+55*2/4"
10th Jul 2022, 10:55 AM
sylviun
sylviun - avatar
+ 2
sylviun Thats completely wrong… You need to add all numbers between num1 and num2 as well
10th Jul 2022, 11:39 AM
Janne
Janne - avatar
+ 2
Thanks All, i understand now.
10th Jul 2022, 8:42 PM
Reiss
+ 2
N = int(input()) x = sum(range(N+1)) using SUM function
24th Oct 2022, 8:56 AM
Rahmanda Eko Nugroho
Rahmanda Eko Nugroho - avatar
+ 2
That works for me: N = int(input()) list = range (0,N+1) sum = 0 for x in list: sum += x x += 1 print (sum)
15th Dec 2022, 1:31 AM
Maksim Khotimchenko
Maksim Khotimchenko - avatar
+ 2
N = int(input()) sum = 0 for x in range(0, N+1): sum+= x print(sum) And That's it!
20th Jan 2023, 4:15 AM
Firoj Paudel
Firoj Paudel - avatar
+ 1
Theres no use of N… But in the loop you need to do: sum += x And in the end: print(sum)
9th Jul 2022, 10:23 AM
Janne
Janne - avatar
+ 1
Let me try that Quantum. Thank you all for your help.
9th Jul 2022, 10:36 AM
Reiss
+ 1
Thanks guys, Herr that worked. I’ve been learning how to code for the last 6 days. I can read basic code for the most part but i cant solve the problems. How long have you been learning for, and how long was it before you felt conpetent?
9th Jul 2022, 10:46 AM
Reiss
+ 1
Reiss I ve been casually coding for years, and still I doubt I can really call myself competent in any language I've learnt so still long road ahead of you. Out don't let it discourage you, just keep your own pace and have fun!
9th Jul 2022, 10:51 AM
Herr Rozwel
Herr Rozwel - avatar
+ 1
Ok thank you for your help. Well done for completing so many courses.
9th Jul 2022, 3:32 PM
Reiss
+ 1
sylviun Yeah but its the sum of consecutive numbers… 1 and 3 would be 6 because 1 + 2 + 3
10th Jul 2022, 3:16 PM
Janne
Janne - avatar