Is_prime not working? [solved]


#1



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



the weirdest thing is that it gives me the same error if i make it == to return False
if i make the range 2 to x-1, it errors out at is_prime(3) saying that it "returns None when it should return True"


inb4 the mod says that i shouldnt have an else in my loop.


Replace this line with your code.


#2

jk i figured it out. gotta pull the else out of the loop. if anyone can explain why it shouldnt be in line with the loop, i'd love an explaination


#3

I think it is because the else is for the if's not inside the loop


#4

It's not perfect but....

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


#5

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