Is_prime(3)


#1

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

print is_prime(3)

The error message is this :"Oops, try again. Your function fails on is_prime(3). It returns None when it should return True. "
i think 3 is include (2,x-1) , but why it return None ,when run the code.


#3

Hello @xhwang,

As you have not formatted your code so Its actually a bit complex to see how your code is structured.

I think problem exist in your final else part.

If you just put an input and test your code against it you will find that your code giving you wrong results.

How to fix this?
Indentation ofelse - clause (use for/else)

or

You can directly return the True value as if a number is a Prime then it should return True.


#4

Hi Greentreemee,
I change the code.it's ok, thank you !
def is_prime(x):
if x < 2:
return False
elif x == 2:
return True
else:
for n in range(2,x-1):
if x % n == 0 :
return False
else:
return True


#5

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

print is_prime(3)

#6

The important part is to understand why your last code was not working.
Was/Is it clear? if not we can discuss it.


#7

good ,i know how to formatted the code in here.:innocent:


#8

It was not formatting problem (CC's formatting), It has problem with else clause that lead to wrong results, The formatting (Indentation do solves it) but its important to understand,why?


#9

Hi greentree ,can you understand chinese ? my englist is very poor.:joy:


#10

i try to understand why for----else loop is ok, but if ....else loop is wrong.


#11

Hey @xhwang, I don't know Chinese :smiley: (I may learn someday)

Back to your last code, as I said to test your code on input..
It fails on 3 , reason is simple, range(2,x-1) -- > range(2,3-1) -- > an empty list.
(for loop does not have anything to loop over)

when you fix this by doing this range(2,x)..Your function will fail on 9.
why it fails on 9 now??
suppose x is 9..
when you loop over it...
n is looping on range(2,9) which is a list... [2,3,4,5,6,7,8]
at first loop n takes value of 2 and as 9 is not divisible by 2, The test x % n == 0 turns out to be False and else code executes and it returns True but It had to return False


#12

Hi greentreemee,
Yes, in the previous test,i get into trouble with "9"......... ,Now, i know why. Thank you for your careful explanation.


#13

i try to understand why for----else loop is ok, but if ....else loop is wrong.

we can go without for/else,
we can just return True as if a number does not qualify for-loop's if test then It must be a Prime number, so we need to return True for this.


#14

without if .... ,just attention the result....., it's a good method for understand if loop statement.
thanks !


#15

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