+ 1
confused
I know this is probably dumb but could someone explain to me how you get the output of 6 out of this code? def func(x): res = 0 for i in range(x): res += i return res print(func(4))
3 Respuestas
+ 5
range(4)=0,1,2,3
0+1+2+3=6
+ 2
For i in range of(x)
I is 1
Res is 0
Res += i
Res is 1
I is 2
Res is 1
Res += 2
Res is 3
I is 3
Res is 3
Res += 3
Res is 6
I is 4, exit loop
Res = 6
I don't know anything about the range function in python so I don't know if the check is exclusive or not, but that's the idea.
+ 1
print(func(4))
for i in range(x) : here I is assigned i = 0,1,2,3,..., x-1 iteratively.
And will be added to res by res = res + i
Edward Hodgson III Code is for sum of 0+1+2+3+4+5+.... So
0+1+2+3=6