+ 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]
2 Answers
+ 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.
+ 1
Thank you very much.Now I understand.