0
how
What is the output of this code? def func(x): res = 0 for i in range(x): res += i return res print(func(4)) the answer for this one is 6 i run it and found 6 but did not understandhow exactly could you help guys???
3 Respostas
+ 2
res begins as 0, for each number in the range loop it will add the value for that iteration.
i begins at 0
i turns 1, res+i =0+1
i turns 2, res+i =1+2 (1 for the result in the last instance)
i turns 3, res+i =3+3, equals 6
+ 2
Res+= i
Is the same thing as res + res= i
So if we ten the loop 4 times we get 6
0
still did not get it bro