Can any body help me know why this adds 3multiple times instead of adding 10 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can any body help me know why this adds 3multiple times instead of adding 10

a = [0,1,2,3,4,5] S=0 for n in a: S += n if(n==3): a.insert(n,10) if(S>15): break Print(s)

29th Mar 2019, 12:18 AM
Thimira Rathnayake
Thimira Rathnayake - avatar
2 Answers
+ 3
Because whenever n is 3, a 10 is inserted at the spot where 3 is. Which means that 3 shifts one slot to the right. Which means that in the next loop we'll meet that 3 again.
29th Mar 2019, 12:37 AM
HonFu
HonFu - avatar
+ 1
Thanks HonFu
29th Mar 2019, 1:04 AM
Thimira Rathnayake
Thimira Rathnayake - avatar