- 1
Please help me to understand this briefly
N = int(input()) count = N x = range(1, N +1) for i in x: N = i + N print(N - count)
7 Antworten
+ 3
Chinmaya Chiranjibi Lenka IX-B - 34
sum is a function and count is a variable. I have used sum as a variable in previous reply but we should not use any identifier as a variable.
+ 4
Chinmaya Chiranjibi Lenka IX-B - 34
This is the solution to sum integer value 1 to N.
So if N is 10 then range(1, N + 1) will give 55 but you are adding N to this range output so total means N will be N = N + 55 = 10 + 55 = 65.
But you have printed N - count so output will be 65 - 10 = 55.
But your this solution is confusing for new learner so here is easy solution
sum1 = 0
for i in range(1, N + 1):
sum1 = sum1 + i
print (sum1)
+ 4
Chinmaya Chiranjibi Lenka IX-B - 34
because range(1, N + 1) means range (1, 11) will return a list with values (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) and the sum of this list will be 55.
Try this in code playground
l = range(1, 11)
print (list(l))
print (sum(l))
+ 3
that's a weird solution to sum up the integers from 1 to N ^^
0
If N is 10 how range (1,N+1) will give 55? plz briefly explain I am time....
0
Is sum a variable?
Is count a variable?
0
Thnx