2
for n in range(2,10):
    for x in range(2,n):
        if n%x==0:
            print(n, 'equals to', x, '*', n//x)
            break
        else:
            print(n, 'is a prime number')

Below is the output I received.

3 is a prime number
4 equals to 2 * 2
5 is a prime number
5 is a prime number
5 is a prime number
6 equals to 2 * 3
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
8 equals to 2 * 4
9 is a prime number
9 equals to 3 * 3

But, it is repeating the values. Moreover 9 is not a prime number. Please help me on correcting the syntax.

Below is the link: https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mohan
  • 91
  • 1
  • 10
  • There is another question like this on SO, it might help you: http://stackoverflow.com/questions/18833759/python-prime-number-checker – Frumples Sep 13 '14 at 06:10

3 Answers3

5

As you have it, your program checks if n is divisible by a factor. If it is, great, you correctly print that it is composite. If it's not, you immediately print it's a prime number. The problem is you don't know that until you've checked the rest of the factors. Just because 9 isn't divisible by 2 doesn't mean it's prime. You have to check 3 as well.

That means changing up your logic a bit. You need to check all of the possible factors before declaring n is prime. One way to do that is with an additional boolean variable.

for n in range(2,10):
    is_prime = True

    for x in range(2,n):
        if n%x==0:
            print(n, 'equals to', x, '*', n//x)
            is_prime = False
            break

    if is_prime:
        print(n, 'is a prime number')

Notice that the "is a prime number" printout isn't inside the inner loop any more, it's outside of it.

Another way to do this is to use a cool Python trick and add an else clause to the inner loop. The else clause only happens if the loop exits without hitting a break statement.

for n in range(2,10):
    for x in range(2,n):
        if n%x==0:
            print(n, 'equals to', x, '*', n//x)
            break
    else:
        print(n, 'is a prime number')

Note that this idea of tacking an else clause onto a loop is a Python-ism. It doesn't exist in other languages like C or Java. So while neat, it's good to understand the boolean variable-based solution as well. That technique will work in any language.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

According to the link you have provided,it has a statement just under the sample code which says

Yes, this is the correct code. Look closely: the else clause belongs to the for loop, not the if statement.

So you should intent the else clause targeting the inner for not if.

zionpi
  • 2,593
  • 27
  • 38
0

I had a difficulty understanding what was actually going on. Found this answer https://stackoverflow.com/a/16451661/5203300

Scott Davis
  • 147
  • 1
  • 4
  • 10