I don't why this code output the numbers more time.If I change the code it raises the error.can anybody solve this problem. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I don't why this code output the numbers more time.If I change the code it raises the error.can anybody solve this problem.

Prime number in given intervals https://code.sololearn.com/c87FRe03Q6Lc/?ref=app

17th Jan 2022, 7:05 AM
แŽทแ’.แˆแŽงแŽดแ‹แ’
แŽทแ’.แˆแŽงแŽดแ‹แ’ - avatar
4 Answers
+ 4
Instead of if-else, try using a for-else clause in inner loop for: if: break else: print()
17th Jan 2022, 7:22 AM
Simba
Simba - avatar
+ 3
Thanks guys problem solved
17th Jan 2022, 7:44 AM
แŽทแ’.แˆแŽงแŽดแ‹แ’
แŽทแ’.แˆแŽงแŽดแ‹แ’ - avatar
+ 2
Thanks for your answers
17th Jan 2022, 7:23 AM
แŽทแ’.แˆแŽงแŽดแ‹แ’
แŽทแ’.แˆแŽงแŽดแ‹แ’ - avatar
+ 1
แŽทแ’.แˆแŽงแŽดแ‹แ’ in nested loop, you are printing that much times the number is not completely divisible(remainder!=0) by number less than it. So lets you have 5 Then 5%2!=0 #print 5 5%3!=0 #print 5 5%4!=0 #print 5 So now you get the issue in the code... Solution: Set a flag which will keep track if nested loop is broken or not. If it broke means no prime otherwise prime...
17th Jan 2022, 7:17 AM
saurabh
saurabh - avatar