I know the output, but I still do not understand why this is the output. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I know the output, but I still do not understand why this is the output.

Could anybody please explain this? def func(x): res = 0 for i in range(x): res += i return res print(func(4))

2nd Jun 2017, 2:35 PM
Mariska Pieters
4 Answers
+ 5
range (4) expands to the list 0, 1, 2, 3 i = 0 res = 0 + 0 = 0 i = 1 res = 0 + 1 = 1 i = 2 res = 1 + 2 = 3 i = 3 res = 3 + 3 = 6 Does that help?
2nd Jun 2017, 1:58 PM
Jason Runkle
Jason Runkle - avatar
+ 4
Actually with the in range syntax you create an array. So your statement could have been as follows: var x[4]. instead
2nd Jun 2017, 2:01 PM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 1
Oh my god, I suddenly see it! Thank you so much :-)
2nd Jun 2017, 2:49 PM
Mariska Pieters
0
It says the answer is 6. I do not see how to get to 6...
2nd Jun 2017, 2:36 PM
Mariska Pieters