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


#1



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


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


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


#2

why do you have range(2,x+1)? why the +1? that doesn't make sense
this bit:

else:
   return True

is wrongly indented, you only want to return True after the loop is finished running, at the moment the loop finishes in the first run


#3

it wont do range(2,2). So I put x+1.


#4

but that is potential dangerous, given you might now include a value which it is not suppose to. Make a special case (first, obviously) for 2


#5

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