Is_prime


#1



I have no idea what's wrong with my code

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096


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


 
print(is_prime(x))


#2

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


#3

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


#4

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.....


#5

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