- 2
Hey, i need a hand for this python code please!
No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers. Let’s save some time by creating a program to do the calculation for you! 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.
14 Answers
+ 3
Where's your attempt?
+ 3
I don't get why you keep posting incomplete codes ..!!
Anyways code for your requirement is below..!!
Feel free to ask if any doubt..!!
n = int(input())
print (n)
res = 0
for i in range(n+1):
res= res+i
print(res)
+ 2
Yup, why are you trying to define another function named sum()? Why and what is that 'x' argument supposed to be?
You may want to go another round at the python lessons. Specifically number ranges and basic syntax in if statements
+ 1
N = int(input())
print(N)
print[1:]
+ 1
Use sum() on the range of (input + 1)
+ 1
Ok
+ 1
N = int(input())
print(N)
def sum(x)
res = 0
for N in range (input +1)
# i've tried it, it keep showin invalid syntax line 3
+ 1
Yeah, i think so. Thanks
+ 1
N = int(input())
print(N)
def sum(x):
res = 0
for N in range (input +1)
0
You missed a " : " at the end of your line 3. That's the reason for invalid syntax at line 3.
Please post your full code..!!
0
N=int(input)
res=0
for i in range(N+1)
res=res+i
print(res)
0
Please add. colon in for loop
0
You could use numpy for that:
import numpy as np
N = int(input()) + 1
nums = np.arange(1, N)
print(nums.sum())
0
N=int(input())
print(sum(range(1,N+1)))