0

Python challenge output

below code prints output value of sum as 18... could you please explain it to me ? b = [0,1,2,3,4,5] sum = 0 for n in b : sum += n if n == 3 : b.insert(n,10) if sum > 15: break print (sum) print (b)

14th Jan 2019, 6:23 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 3
10 is inserted at position 3, but then the loop goes on with index 4, where 3 is sitting now, so it will be added again. 1+2+3+3+4+5=18
14th Jan 2019, 6:59 PM
HonFu
HonFu - avatar
+ 1
deleted duplicate entry.... expecting explanation of code output from any1
14th Jan 2019, 6:38 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
oh...I got it now.. looking after your result...thanks... I missed that sum is updated before updating list...
14th Jan 2019, 7:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar