+ 2

I did the Python Challenge but didn't understand why this code got the answer.

A = [1,2,3,4,1] for n in A: A[n] = 0 print(A) Answer = [0,0,3,0,1]

1st Oct 2019, 12:47 AM
Aattawut Nutlamyong
Aattawut Nutlamyong - avatar
2 odpowiedzi
+ 5
1st iteration : [1,2,3,4,1] ¯ n=1 A[1] = 0 [1,0,3,4,1] 2nd iteration : [1,0,3,4,1] ¯ n=0 A[0] = 0 [0,0,3,4,1] 3rd iteration : [0,0,3,4,1] ¯ n=3 A[3] = 0 [0,0,3,0,1] 4th iteration : [0,0,3,0,1] ¯ n=0 A[0] = 0 [0,0,3,0,1] 5th iteration : [0,0,3,0,1] ¯ n=1 A[1] = 0 [0,0,3,0,1] So, the tricky part is that array changes after each iteration and therefore value of variable n changes.
1st Oct 2019, 1:22 AM
voja
voja - avatar
+ 1
Thank you very much.Now I understand.
1st Oct 2019, 1:32 AM
Aattawut Nutlamyong
Aattawut Nutlamyong - avatar