I have written a code to check for the total number of prime numbers up to and including a certain no. When performing the check for the given number as 3, a change in the indentation of the else statement is changing my output. I cannot understand why is that happening; please explain. I need output as [2,3].
def count_prime(num):
lis=[x for x in range(3,num+1,2)]
lisi=[2]
check=False
if num==0 or num==1:
return 0
for a in lis:
for b in range(3,a,2):
print(b)
if a%b==0:
check=False
break
else:
print('here is indentation change')
check=True
pass
if check==True:
lisi.append(a)
primes=len(lisi)
print(lisi)
return (primes)
This gives the list of primes as 2 and no. of primes=1 for num=3
def count_prime(num):
lis=[x for x in range(3,num+1,2)]
lisi=[2]
check=False
if num==0 or num==1:
return 0
for a in lis:
for b in range(3,a,2):
print(b)
if a%b==0:
check=False
break
else:
print('here is indentation change')
check=True
pass
if check==True:
lisi.append(a)
primes=len(lisi)
print(lisi)
return (primes)
This gives the list of primes as [2,3] and no. of primes=1 for num=3
Please click the below links to see the images of the codes