Is_prime


#1

Hey there,
i'm stuck at this exercise: https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#


This is the error message i get:
Oops, try again.
Your function fails on is_prime(9). It returns True when it should return False.


I dont quite get it. My code and comments below:

 
def is_prime(x): #lets take 9 as the example
    if x < 2: #pass
        return False
    elif x == 2: #pass
        return True
    else:    
        for n in range(2, x -1):
            if x % n == 0: #here it should give me the return False, because 9%3 is 0, isnt it? Why is the error message that it returns True?
                return False
            else: 
                return True

Thanks for all your help!


#2

There was an exact same topic earlier today:


#3

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