I cant understand the difference between those two code blocks, first the correct one is:
number_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
prime_list=[]
for i in number_list:
for j in range(2,i):
if i%j==0:
break
else: prime_list.append(i)
print('Primes are:',prime_list)
Output is:
Primes are: [1, 2, 3, 5, 7, 11, 13]
but moving the else statement forward inside the block below the if statement (which i thought was the right thing to do) results in a different and wrong output:
number_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
prime_list=[]
for i in number_list:
for j in range(2,i):
if i%j==0:
break
else: prime_list.append(i)
print('Primes are:',prime_list)
Output is:
Primes are: [3, 5, 5, 5, 7, 7, 7, 7, 7, 9, 11, 11, 11, 11, 11, 11, 11, 11, 11, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13]
Why is this so? how does the code behave with the else statement indented?