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?
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
n = int(input()) x = 0 # important declaration.. for i in range(1,n+1): x += i print(x)
The for loop should look like this...... for x in range(1, N + 1):
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...
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
The answer to your problem using three different methods: https://code.sololearn.com/crKOSyW75BLv/?ref=app
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.
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
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"
sylviun Thats completely wrong… You need to add all numbers between num1 and num2 as well
Thanks All, i understand now.
N = int(input()) x = sum(range(N+1)) using SUM function
That works for me: N = int(input()) list = range (0,N+1) sum = 0 for x in list: sum += x x += 1 print (sum)
N = int(input()) sum = 0 for x in range(0, N+1): sum+= x print(sum) And That's it!
Theres no use of N… But in the loop you need to do: sum += x And in the end: print(sum)
Let me try that Quantum. Thank you all for your help.
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?
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!
Ok thank you for your help. Well done for completing so many courses.
sylviun Yeah but its the sum of consecutive numbers… 1 and 3 would be 6 because 1 + 2 + 3
Python — File Handling
Question is write a c program to print prime numbers up to n and print the largest number in array.
Achievements on Sololearn
How to draw in the console?