6 - is_prime


#1

Why do I get an error? It says it creates a loop?!?

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

When I run this in PyCharm it works just fine.
Just the number 2 is a problem.


#2

I ran your code because im stuck on the same one and an error box popped up and said "The program took too long to finish. Check your code for infinite loops and try again." So what do you mean by your statement at the end of your post?


#4
def is_prime(x):
if (x > 1): # x must be greater than 1
    for n in range(2,(x - 1)): # range 2 - 1 less than x
        print n # So you can see which n gets the job done
        if x % n == 0:
            print x, "is NOT prime, Dummy"
            return False
else:
    print x, "is NOT prime, Dummy"
    return False
print x, "is prime. Good Choice!"
return True

is_prime(279481) #This one may take a while...not infinite loop


#7

Hi, shablama.
Your code enters a loop because if the user enters an argument greater than 1, your while statement will loop forever.
Instead of creating a loop, why not just work with "if/elif/else" statements?

I've written the code below. It might just do the trick you're looking for:

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
    return True

However, I still don't completely know why this works and why the code below doesn't:

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

If anyone could shed light into that matter, I'd appreciate it.

Cheers.


#8

Did anybody shed light into the matter ?


#9

I have the same question.


#10

I don't get it. I run the code in PyCarm multiple times and no infinite loop (the loop is broken by the "if statement".
The code just runs fine with the only exception that the number 2 (print is_prime(2)) will return nothing.

also. the code below runs also fine in PyCharm:

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

maybe a bug in codecademy?


#11

May i suggest you put "break" after return false in your if statement.

   for n in range(2,x):
        if x % n == 0:
            return False
            break
        else:
            return True

#12

Does anyone know why this code works:

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
                    
        return True

but this doesn't:

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

I would think that the first version of the program would always return True regardless of the input, because it's not nestled in any if condition. How come it doesn't do that?


#13

Hi again!
I've ran your code in Python and I get infinite loop if I enter 2 as an argument to your function.

As to whether there is a bug in codecademy, I don't think this is the case, because my second code also gives me a wrong answer in Python, if I input 9 as an argument. I don't know why this happens.

Cheers.


#14

Could someone help?this is my code.it returns error for input x =9.I am getting False but the interpreter says true.i can't make it out why.any help would be welcome.thanks.plus,how do i indent?

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


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

this is the right answer.
your second one has a problem in if and else statement.
for the first loop, n == 2, if x == 9, obviously x % n is not 0, so it turn to else statement and return True, and the loop is break, but 9 is not a prime number. the problem is that if and else statement runs in every loop, but you want the else statement to run after the loop, so you should place the else after the for loop like my answer


#16

Thank you! Now I understand it! :smile:


#17

Great,just help to shed light to my confusion!:grinning: