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?

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

+ 8

n = int(input())
x = 0 # important declaration..
for i in range(1,n+1):
x += i
print(x)

+ 6

The for loop should look like this......
for x in range(1, N + 1):

+ 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...

+ 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

+ 5

The answer to your problem using three different methods:
https://code.sololearn.com/crKOSyW75BLv/?ref=app

+ 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.

+ 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

+ 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"

+ 2

sylviun Thats completely wrongâ€¦ You need to add all numbers between num1 and num2 as well

+ 2

Thanks All, i understand now.

+ 2

N = int(input())
x = sum(range(N+1))
using SUM function

+ 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)

+ 2

N = int(input())
sum = 0
for x in range(0, N+1):
sum+= x
print(sum)
And That's it!

+ 1

Theres no use of Nâ€¦ But in the loop you need to do: sum += x
And in the end:
print(sum)

+ 1

Let me try that Quantum. Thank you all for your help.

+ 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?

+ 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!

+ 1

Ok thank you for your help. Well done for completing so many courses.

+ 1

sylviun Yeah but its the sum of consecutive numbersâ€¦ 1 and 3 would be 6 because 1 + 2 + 3

Hot today

Python â€” File Handling

2 Votes

Help me

0 Votes

Whatâ€™s wrong?

2 Votes

Question is write a c program to print prime numbers up to n and print the largest number in array.

1 Votes

Achievements on Sololearn

1 Votes

How to draw in the console?

0 Votes