Is_prime


#1



is_prime


Oops, try again. Your function fails on is_prime(0). It returns None when it should return False.


def is_prime(x):
    for n in range(2,x-1):
        if x<2:
            print false    
            break
        elif x % n==0:
            print false
            break
        else:
            print true


#2

range does not include its stop value, so not sure why you would subtract one from x for range its stop value

your if condition is quit useless, i mean, the minimum value for your loop to run is 4, so checking if x is less then 2 in the loop won't work

maybe you should check if x is less then 2 before the loop?


#3

def is_prime(x):
    if x>2:
        
        for n in range(2,x-1):
            if x % n==0:
                print False
                break
            else:
                print True
        else :
        print False

`


#4

your function needs to return if the number is a prime number (return True) or not (return False)

important to understand is that return will end a function, even when in a loop (the loop simply breaks so the function can end)


#6

Try removing an indentation from your last else statement. You only want the final else statement to trigger if all numbers between 2 and x-1 fail to divide x evenly. As it stands right now, the else statement is going to run every time the loop starts over.

What the for-loop does is go through every number n between 2 and X-1. If x is evenly dividable by n, it returns True because it means x is not a prime. If x is not evenly dividable by n, it starts the loop over with n+1 and does the same test again.

If the for loop manages to finish the range 2, x-1 without breaking, that means that the number must be a prime, in which case you want it to exit the loop and return True.


#7

Oops, try again. Your function fails on is_prime(0). It returns None when it should return False.

def is_prime(x):
    if x>2:
        
        for n in range(2,x-1):
            if x % n==0:
                return False
                break
            else:
                return True
    else :
        print False

#8

    else :
        print False

still prints False, not returning False for final else clause


#9

Oops, try again. Your function fails on is_prime(2). It returns False when it should return True.

def is_prime(x):
    if x>2:
        
        for n in range(1,x-1):
            if x % n==0:
                return False
                break
            else:
                return True
    else :
        return False

#10

when x=2, the if condition is false:

if 2 > 2:

so else runs, which returns False, but 2 is a prime number


#11

i corrected this mistake ,now it returns none

def is_prime(x):
    if x>=2:
        
        for n in range(2,x-1):
            if x % n==0:
                return False
            else:
                return True
    else :
        return False

#12

make sense, the range currently is:

range(2, 1)

when x=2, so the loop condition is false the loop never runs

So nothing is returned

The number is only a prime number if the whole loop finishes running, so you want to only return True after the loop has finished running, currently the function ends in the first iteration of the loop (return will end the function, and breaks the loop if needs be)


#13

soo what is the correct range for this problem ?


#14

move return True outside the loop, you then have the look that for x=2 and x=3 will also be managed by this, despite the loop not running


#15

Oops, try again. Your function fails on is_prime(9). It returns True when it should return False.

def is_prime(x):
    if x>3:
        
        for n in range(2,x-1):
            if x % n==0:
                return False
            else:
                return True
    elif x==2 or x==3 :
        return True
    else :
        return False

#16

i got my mistake !!
solved !

def is_prime(x):
    if x>3:
        
        for n in range(2,x-1):
            if x % n==0:
                return False
            
        return True
    elif x==2 or x==3 :
        return True
    else :
        return False

#17

thanks for helping :slight_smile:


#18