+ 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??
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)
+ 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..
+ 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 😬👌
+ 2
Sharareh heyati Do what Jayakrishna🇮🇳 suggested and remove the if statement
+ 1
In addition:
Check your indentation.
Check the spelling: it is "range"
+ 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)
+ 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)
+ 1
Again: Check the INDENTATION, indentation is crucial in Python.
S doesn't serve any purpose, remove it.
Set s = 0 in the beginning
+ 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.
+ 1
Yes thats true so what can i do😒
+ 1
AGAIN: ⭐INDENTATION⭐ IS CRUCIAL IN PYTHON. Check your indentation.
+ 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
+ 1
Thanks I do finally 😬👌
I had one space before print
So I removed it and ✔
+ 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
+ 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
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)
0
I do the Jayakrishna🇮🇳 suggeste but the print line have an error
0
Ok. Im going to ✔
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