I have no idea what's wrong with my code

It does not return true for is_prime(2)

x=int(input("Please enter an integer to see if it's prime:\n"))

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



Consider which steps it should be carrying out in order to determine whether the number is a prime

Then add prints which prints out what it's doing

If your algorithm is correct and it prints the same steps that you intended, then the function is also correct for that input. If your function is not correct for that input, then it's going to print some other steps and you can see where it goes wrong


Remove the elif condition. Instead, put return True outside the for loop.


from math import sqrt
def is_prime(x):
----if x < 2:
--------return False
----for n in range(2,int(sqrt(x))+1):
--------if x % n == 0:
------------return False
--------return True

this works for me, but i don't understand the x-1 part in the instruction.....


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