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???

12th Jul 2017, 11:52 PM
Mohammed Heyouni
Mohammed Heyouni - avatar
3 Answers
+ 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
13th Jul 2017, 1:58 AM
Normanefe
Normanefe - avatar
+ 2
Res+= i Is the same thing as res + res= i So if we ten the loop 4 times we get 6
13th Jul 2017, 12:34 AM
Yoni S
Yoni S - avatar
0
still did not get it bro
13th Jul 2017, 12:37 AM
Mohammed Heyouni
Mohammed Heyouni - avatar