Is_prime


#1

Help to understand some topic:
1 My loop is not working because of the return value. Its only looping once
so what would be indentation of return value in the loop
2 should i define x==2 in loop or outside it or just "for loop" will be good
Is this correct for the loop indentation of return

    for n in range(2, x):
        if  x % n == 0:
               return False

And any reference to understand this topic
Thanks for help


#2

Your loop is fine, though the indentation looks a little off.


def is_prime(x):
    if x < 2: return False
    for n in range(2, x):
        if x % n == 0:
            return False
    return True

This is the brute force approach suggested in the instructions. It is adequate for our present needs.

When writing indentation, use the recommended four spaces, and not the Tab.

No, not necessary. 2 will not enter the loop because the range will be, 2..1 so the loop will not run. Flow will drop to the last line where 2 will return True.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.