Can anyone help me understand why my program of finding primes doesn't yield satisfactory results? | Sololearn: Learn to code for FREE!
+ 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')

5th Nov 2017, 2:43 PM
a2warik
a2warik - avatar
4 Antworten
+ 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')
5th Nov 2017, 3:15 PM
Muthumani V
Muthumani V - avatar
+ 2
How do you reset count after it is bigger than 1? I think count = 0 should be moved one line lower.
5th Nov 2017, 3:13 PM
Paul
Paul - avatar
0
thank you. it seems my error was only nesting the if loop incorrectly.
5th Nov 2017, 3:27 PM
a2warik
a2warik - avatar
0
thank you both Paul and Muthumani for pointing my mistakes
5th Nov 2017, 3:29 PM
a2warik
a2warik - avatar