Hey, i need a hand for this python code please! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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.

8th Jun 2022, 11:10 AM
Aliyu Tukur
Aliyu Tukur - avatar
14 Answers
+ 3
Where's your attempt?
8th Jun 2022, 11:23 AM
Slick
Slick - avatar
+ 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)
9th Jun 2022, 1:19 PM
Sasi Kumar
Sasi Kumar - avatar
+ 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
8th Jun 2022, 11:37 AM
Slick
Slick - avatar
+ 1
N = int(input()) print(N) print[1:]
8th Jun 2022, 11:25 AM
Aliyu Tukur
Aliyu Tukur - avatar
+ 1
Use sum() on the range of (input + 1)
8th Jun 2022, 11:27 AM
Slick
Slick - avatar
+ 1
Ok
8th Jun 2022, 11:27 AM
Aliyu Tukur
Aliyu Tukur - avatar
+ 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
8th Jun 2022, 11:32 AM
Aliyu Tukur
Aliyu Tukur - avatar
+ 1
Yeah, i think so. Thanks
8th Jun 2022, 11:39 AM
Aliyu Tukur
Aliyu Tukur - avatar
+ 1
N = int(input()) print(N) def sum(x): res = 0 for N in range (input +1)
9th Jun 2022, 1:08 PM
Aliyu Tukur
Aliyu Tukur - avatar
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..!!
9th Jun 2022, 1:40 AM
Sasi Kumar
Sasi Kumar - avatar
0
N=int(input) res=0 for i in range(N+1) res=res+i print(res)
9th Jun 2022, 1:22 PM
Rizwan Ahmad
Rizwan Ahmad - avatar
0
Please add. colon in for loop
9th Jun 2022, 1:23 PM
Rizwan Ahmad
Rizwan Ahmad - avatar
0
You could use numpy for that: import numpy as np N = int(input()) + 1 nums = np.arange(1, N) print(nums.sum())
10th Jun 2022, 6:20 AM
Alvaro GV
0
N=int(input()) print(sum(range(1,N+1)))
12th Jun 2022, 2:39 AM
Pavan Barve
Pavan Barve - avatar