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


#1



def is_prime(x):
    n =2
    while 2 <= n <= x-1 and 2 <= x:
        if x < 2 :
            return False
            
        elif  x % n == 0 and x == n:
            print x
            return False
            x += 1
            
        else:
            print x
            return True
            x += 1
        n += 1

Heading


#2

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


#3

You are somewhat overthinking this. Try following this template:

def FUNCTION(PARAMETER):
    if PARAMETER < INTEGER:
        return BOOLEAN
    elif PARAMETER >= INTEGER:
        for INDIVIDUAL_ITEM in range(INTEGER, PARAMETER - 1):
            if PARAMETER % INDIVIDUAL_ITEM == 0:
                return BOOLEAN
        else:
            return BOOLEAN

If you have any questions at all on how or why this works, please just ask.


#4

I used this same method and the function doesn't work when the parameter is 4 it returns the wrong boolean value


#5


#6

You probably made a syntax error somewhere. I'll need to see your code if I am going to help you fix it.


#7

yes my else wasn't indented correctly it was as simple as that


#8

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