+ 3
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?
36 Respostas
+ 19
n = int(input())
x = 0 # important declaration..
for i in range(1,n+1):
x += i
print(x)
+ 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
+ 7
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...
+ 6
The for loop should look like this......
for x in range(1, N + 1):
+ 6
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.
+ 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
+ 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
+ 3
sylviun Thats completely wrong… You need to add all numbers between num1 and num2 as well
+ 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
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
Rajeev Sharma why x+= 2? You need to do x += i