6. is_prime


#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.

I can't figure this one out. I thought that x % n == 0 could handle is_prime(9). I was reading other people's questions about this exercise, and the moderators talked about how a function ends when a return keyword is reached. How can I fix mine so that this doesn't happen?


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
            elif x % n != 0:
                return True


#2

Hey there! I think your question looks a lot like this topic:

And the second post there asnwers the question:


Good luck!


#3

def anti_vowel(text):
k = []
s = ""
for i in range(0,len(text),1):
if text[i] == "a" or text[i] == "A" or text[i] == "e" or text[i] == "E" or text[i] == "i" or text[i] == "I" or text[i] == "o" or text[i] == "O" or text[i] == "u" or text[i] == "U":
k.append("")
else:

        k.append(text[i])
s = "".join(k)
return s

this is the pimpliest way to understand and to do this


#4

We're not supposed to give the answer, @kathan420. Especially not if it is wrong...


#5

ohk
i won't give next time


#6

Okay, I figured it out. Thanks for the help!


#7

hi , this is may code and it's working.

def is_prime(x):
    p = 2
    if x == 0 or x == 1:
        return False  
    elif x < 0:
            return False    
    while p <= x:
        if x > p and x % p == 0:
            return False
        elif p == x:
            return True
        p += 1
print is_prime(-10)