0
Shouldn't this code print out the items in my list instead of what its printing out
a=[1,2,3,4,1] for n in a: print(n)
5 Réponses
+ 4
Here n will iterate through each element in the list, Starting from index 0 upto index 4
step 1)element at index 0 in [1,2,3,4,1] is 1 therefore n=1
a[n]=0 i.e. a[1] = 0 so now the list becomes [1,0,3,4,1]
print(n) will print 1
step 2)element at index 1 in [1,0,3,4,1] is 0 therefore n=0
a[n]=0 i.e. a[0] = 0 so new list is [0,0,3,4,1]
print(n) will print 0
step 3)element at index 2 in [0,0,3,4,1] is 3 therefore n=3
a[n]=0 i.e. a[3] =0 so new list is [0,0,3,0,1]
print(n) will print 3
step 4)element at index 3 in [0,0,3,0,1] is 0 therefore n=0
a[n]=0 i.e. a[0]=0 so new list is [0,0,3,0,1]
print(n) will print 0
step 5)element at index 4 in [0,0,3,0,1] is 1 therefore n=1
a[n]=0 i.e. a[1]=0 so new list is [0,0,3,0,1]
print(n) will print 1
+ 3
It should.
What is it printing out instead?
+ 2
Sotomi Oluwadmilola, you can not leave out a line of code and expect the output to be the same!
How could anybody give a reasonable answer to your original post?
0
a=[1,2,3,4,1]
for n in a:
a[n]=0
print(n)
Output = 1
0
3
0
1
That is the full code. Line three was missing. But shouldn't the output still be items in my list??
0
HonFu Sorry typo error