I am very new to Python. I just learnt about for and while loops. There was this exercise given to us to print all prime numbers from a list. I did the following
my = [4,5,7,16,17,35,20,37]
for i in my:
j = 2
while j < i:
if i % j == 0:
break
j = j + 1
else:
print(i)
And this worked but since i am just learning, out of curiosity I tried to achieve the result alternatively
Instead of filtering and disallowing numbers as non prime in the if
condition, I tried to do opposite ie evaluating numbers as prime in the if
condition and use else to print non occurrence or prime number
my = [4,5,7,16,17,35,20,37]
for i in my:
j = 2
while j < i:
if i % j != 0:
print(i)
j = j + 1
else:
print("Prime No not found")
The issue with with this code it, its prints (i) as soon the first instance of j satisfies the if condition. In the if statement, ideally I want it to first complete the while loop for j and print (i) only if all of the instance of j
satisfies ( i % j is != 0
)
With my current knowledge, I am unable to fix it. Can someone please rectify my code?
PS: I don't want a totally different or a advanced answer, just need changes in mine itself so i can understand at my beginner level.
In short, i am basically trying to use the if condition to store prime numbers and else for non prime, Unlike my first working code where i did the opposite
It's not possible to reverse the logic from the first version of your code. You need to run the whole inner loop before you can know the number you are testing is prime. You might find that it's not prime early (letting you quit and not test the rest of the potential divisors), but there's no instant check for primeness that you can do inside the inner loop. If there was, you wouldn't need the loop at all!
Any effort to "fix" your second code would just turn it in to the first version you started with. There are better prime-testing algorithms, but they're mostly a lot more complicated, and would fundamentally change the structure of your code.
Observe the comments I implemented in the code:
my = [4,5,7,16,17,35,20,37]
for i in my:
j = 2
while j < i:
if i % j == 0: # As long as there's a single division that results in 0 remainder, the number is not prime
break
j = j + 1
else:
print(i)
Since you don't have a break
statement inside the while
loop below, the else
statement will always be initiated. That is the whole point of an while
else
statement. So you need to add a break
somewhere.
my = [4,5,7,16,17,35,20,37]
for i in my:
j = 2
while j < i:
if i % j != 0: # If a number is prime, all the cases will result in non-zero remainder, so many numbers will be print out
print(i)
j = j + 1
else:
print("Prime No not found")
my = [4,5,7,16,17,35,20,37]
for i in my:
j = 2
while j < i:
if i % j != 0:
print(i)
break # you need to put the break out of if statement,so it will go to the next number to test, Hope this help
else:
print("Prime No not found")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.