Hi to the forum;
A simple question:
I want to make a prime number checker:
So if I want to test if 25 is a prime number.
i should to verify if 2 is a factor.
if it is not, i verify with 3 and 4 (not my subject if not 4 id included in not 2), and finally 5.

with a for loop,

for i in range (2, 25):
    if 25 % i == 0:
        break
    else:
        print (25, " is a prime number.")

is a wrong code because after testing 25 % 2, the conclusion where that 25 is a prime number.
So it is imperative that the condition controlled loop run's from 2 to 24(I know the exageration but It's not my subject).

What is the right procedure?

Recommended Answers

The else part must go with the for. It is only executed if the for loop was not interrupted by a break statement

for i in range (2, 25):
    if 25 % i == 0:
        break
else:
    print (25, " is a prime number.")

See the doc

Jump to Post

All 3 Replies

Thank so much!
It is precisely the response!
It is a simple trivial question but i don't regret. It's ggod to read a good answer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.