+ 2

How the answer is 6 ?

def func(x): res = 0 for i in range(x): res += i return res print(func(4))

7th Mar 2018, 10:56 AM
Venkatesh
2 Antworten
+ 14
for i in range(4) //this loop will run for i=0,1,2,3 res +=i //res +=(0+1+2+3) = 6 //try this code def func(x): res = 0 for i in range(x): res+=i print (res) return res print(func(4))
7th Mar 2018, 12:57 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
Your not adding 1 each time. you are adding the iteration. 0+1+2+3=6
7th Mar 2018, 11:06 AM
LordHill
LordHill - avatar