+ 3

# Can anyone help me understand why my program of finding primes doesn't yield satisfactory results?

count=0 for p in range (2,50): for i in range (2,p+1): if p%i==0: count+=1 else: continue if count<=1: print (str(p)+'is a prime')

4 Answers

+ 3

Try the below code and you should input number. Here variable 'p' is input.
p = 7
count=0
if(p>1):
for i in range (2,p):
if (p%i==0):
count=count+1
else:
continue
else:
print('p<=1')
if(count>1):
print(str(p)+'is not prime')
else:
print(str(p)+ 'is a prime')

+ 2

How do you reset count after it is bigger than 1? I think count = 0 should be moved one line lower.

0

thank you. it seems my error was only nesting the if loop incorrectly.

0

thank you both Paul and Muthumani for pointing my mistakes