+ 5

Hi there please fix my answer😬

This is question in python 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. 🍃🍃🍃 My answer is: N = int(input()) #your code goes here N= int(input()) S= ((N+1)*(N//2)) for i in rang(1,n+1): print(S) How can i fix??

10th May 2022, 1:31 PM
Sharareh.H
Sharareh.H - avatar
19 Réponses
+ 7
Sharareh heyati , see some hints to get the code fixed: N = int(input()) N= int(input()) # this is douplicated, remove it S= ((N+1)*(N//2)) # this is the formula to calculate the sum, but i suppose you should learn it by using a loop. so remove it S = 0 # we need to declare and initialize a variable that will hold the total for i in rang(1,n+1): # spelling of n is not correct, spelling of rang is not correct print(S) # not required indide the loop, remove it S += i # add the numers of the range here # finally print the result stored in S outside the loop here print(S)
10th May 2022, 2:28 PM
Lothar
Lothar - avatar
+ 3
Is you are taking 2 times input? Why? It only have single input. N*(N+1) //2 formula then you don't need loop. Just print this result. Hope it helps..
10th May 2022, 1:58 PM
Jayakrishna 🇮🇳
+ 2
N = int(input()) #your code goes here sa = 0 for i in range(1, N+1): sa += i if (sa >= 100): print(sa) These code work 😬👌
10th May 2022, 5:21 PM
Sharareh.H
Sharareh.H - avatar
+ 2
Sharareh heyati Do what Jayakrishna🇮🇳 suggested and remove the if statement
10th May 2022, 5:31 PM
Lisa
Lisa - avatar
+ 1
In addition: Check your indentation. Check the spelling: it is "range"
10th May 2022, 2:13 PM
Lisa
Lisa - avatar
+ 1
Ok everyone I try to fixed this Q like this but i have an error again☺ N = int(input()) #your code goes here S= ((N+1)*(N//2)) s=1 for i in range(1, N+1): s+=i print(s)
10th May 2022, 3:04 PM
Sharareh.H
Sharareh.H - avatar
+ 1
By using loop now, you don't need to calculate S = (N+1) *N//2 remove it Add identation for s+=I N = int( input()) s=0 #set to 0, not 1 for i in range(1, N+1) : s += i print(s)
10th May 2022, 3:10 PM
Jayakrishna 🇮🇳
+ 1
Again: Check the INDENTATION, indentation is crucial in Python. S doesn't serve any purpose, remove it. Set s = 0 in the beginning
10th May 2022, 3:11 PM
Lisa
Lisa - avatar
+ 1
Read the task description carefully: You always need to print the result, not only when sa >= 100. Suppose N was 4. Then sa would be < 100 and the result would not be printed.
10th May 2022, 5:27 PM
Lisa
Lisa - avatar
+ 1
Yes thats true so what can i do😒
10th May 2022, 5:30 PM
Sharareh.H
Sharareh.H - avatar
+ 1
AGAIN: ⭐INDENTATION⭐ IS CRUCIAL IN PYTHON. Check your indentation.
10th May 2022, 5:37 PM
Lisa
Lisa - avatar
+ 1
Add statement like I added in my post. . Without space for print(sa) when without if. edit: Sharareh heyati see this code for idenation, if you need https://code.sololearn.com/cT5BRIbkia21/?ref=app
10th May 2022, 5:49 PM
Jayakrishna 🇮🇳
+ 1
Thanks I do finally 😬👌 I had one space before print So I removed it and ✔
10th May 2022, 5:54 PM
Sharareh.H
Sharareh.H - avatar
+ 1
https://code.sololearn.com/c2j0TUoL01oE/?ref=app Easier code😄 (only 3 lines) using formula s=n(n+1)/2. I was thought to avoid loop whenever we can solve using arithmetic
12th May 2022, 12:27 PM
Vkd
Vkd - avatar
+ 1
I was interested in this task, so I rewrote your code here:› ## en=int(input()) s=0 cou=0 while (en>=1): s=s+en en-=1 cou+=1 print(str(cou)+". "+str(s)+" + "+str(en)) #you can remove this line and change it to "print(s)" after "while", if you don't want to see all solution
12th May 2022, 1:20 PM
xXxWeb killeRxXx
xXxWeb killeRxXx - avatar
0
#why you need if (sa>=100) : condition? its no output if input is less than 14.. You just need . N = int(input()) sa = 0 for i in range(1, N+1): sa += i print(sa)
10th May 2022, 5:26 PM
Jayakrishna 🇮🇳
0
I do the Jayakrishna🇮🇳 suggeste but the print line have an error
10th May 2022, 5:34 PM
Sharareh.H
Sharareh.H - avatar
0
Ok. Im going to ✔
10th May 2022, 5:47 PM
Sharareh.H
Sharareh.H - avatar
0
U either use formula or add all the elements To add the elements initialize a variable as zero add each element to the variable using a loop and print the sum atlast
11th May 2022, 4:30 PM
Indhu mohan