Is Prime


#1


Hi guys! i've got a little problem with my code. I really can't understanding what's wrong.
Can you help me please ?
Thanks.

Oops, try again. Your function fails on is_prime(5). It returns False when it should return True.


def is_prime(x):
    if x==0 or x==1:
        return False
    elif x==2:
        return True
    else:
        for n in range(2,x-1):
            if abs(x/n-round(x/n))==0:
                return False
                break
        return True


#2

This is the code that I used for the function:

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

I don't really understand what you're doing in the last 'if statement' but I figured it's meant to do the same as what mine is.

Your code is mostly correct but it just seems you took a long way around it and confused.

Hope this helps help you understand where you went wrong!


#3

Hi,
range(1,6) gives 1,2,3,4,5
so if we want to check if x is divisble by any number in the range 2 to x-1, while write in coding format shouldn't we write it as for n in range(2, x)
Why did u write for n in range(2, x-1)


#4

Oh that was just a typo that I never noticed cause the program worked fine. Thanks for pointing that out!


#5

Fix it! :slight_smile:
Thanks for your help guys! very useful


#6

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

Error : Fails for is_prime(9)


#7

Hi ! a found out mistake
the last sentence : "return True" is not inside the last if statement but is outside. delete the final "else" and leave "return True" in the for.


#8

Thanks a ton!!! That works.


#9

Here is the correct foramt

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

#10

for n in range(2, x-1):


#11

Help me troubleshoot this.


#12

try to change :
for n in range(2,x-1):
in:
for n in range(2,x):
(the last number is not inclusive)
Also, delete the else-statement in row 10, "return True" is in the for-cycle not in the if-cycle like in your code.


#13

Oh thank you, Sir, it worked!

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


#14

Hi guys! I am having a little problem getting my code to work. I've checked it against the other code here in the QA forum and I do not see where I went wrong. Thanks for the help and taking a look at it!

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


#15

I found it! my return True was indented within my if statement.


#16

Thanks, been surfing on the page to find the correct one :innocent:


#17

Why does "return True" has to be outside the if statement?


#18

Because: " if you don't find NO NUMBER in range(2,x) " not only for that case. if "return True" is inside the if statement the scrip falls.


#19

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