+ 1

Can anyone make an explanation about this code?

def func(x): res = 0 for i in range(x): res += i return redprint (func(4))Result: 6This was one of the questions I had during the quizzes. Well I saw the answer because of the comments but I didn't find a good reason to believe that's really truth.

31st Jul 2016, 1:11 AM
Jhonne Kleyton
Jhonne Kleyton - avatar
3 Respostas
+ 1
range(4) will create a range of 0,1,2,3 since it is zero based. Expanding we will do: res += 0 #res is 0 res += 1 #res is 1 res += 2 #res is 3 res += 3 #res is 6
31st Jul 2016, 1:20 AM
Eric Kozub
+ 1
iam not 100% sure and i have asked that question before you can replace i by x and gives the same answer then res+=i is mean you multiply the var. x by 2
31st Jul 2016, 4:37 PM
Ahmed Kamal
Ahmed Kamal - avatar
- 1
that is the answer
3rd Aug 2016, 12:11 PM
Cristian Herrera