Is_prime doesnt work for is_prime(9)


#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(9). It returns True when it should return False.


beacause 9 % 3 = 0 it should return False


Replace this line with your code. 
def is_prime(x):
    if x < 2:
       return False
    elif x == 2:
         return True
    else:
         for n in range(2,x):
             if x % n == 0:
                return False
             else:
                  return True


#2

those lines should not be inside the loop. Drop the else and move the return all the way to the left (of the code block, line up with first if).

These three lines are not necessary:

2 will not fit the range of the loop so will drop down to the bottom.


#3

Thank you, i was stuck with this exercise for days :smiley:


#4

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