-3
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
            else:
                return True

This doesn't work for number 9, where it returns true. Please explain in basic terms.

vaultah
  • 44,105
  • 12
  • 114
  • 143
12johnny
  • 71
  • 2
  • 8
  • You are deciding if prime or not in the first loop itteration. The `return true;` has to be called when the loop has ended. – AntiHeadshot Nov 04 '15 at 08:34

1 Answers1

3

You have to remove the else condition in the for loop. For example: for the input: 9 when execution enters the for loop the condition checked is 9 % 2 == 0. Since, it is False, the execution will continue to the else part and will return the value True. What it did not do is continue checking the divisibility with the other numbers. Hence, if the condition is not True, it should simply continue checking divisibility with the other numbers.

Code:

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
        return True
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
  • 2
    > Please explain in basic terms – vaultah Nov 04 '15 at 08:35
  • Yes. I am editing it. – JRodDynamite Nov 04 '15 at 08:36
  • 1
    ["You are welcome to post a first version of your answer as fast as you can, provided it is already a correct and useful answer meeting our minimal standards."](http://meta.stackoverflow.com/questions/303446/is-it-bad-practice-to-post-a-short-answer-first-and-then-add-details-afterwards/303449#303449) – vaultah Nov 04 '15 at 08:40
  • @vaultah - That is exactly what I was doing. I posted the code and then edited it to provide the explanation. Where exactly did I go wrong? – JRodDynamite Nov 04 '15 at 08:51
  • Code-only answers don't meet our standards and are discouraged. – vaultah Nov 04 '15 at 08:54
  • What does the last statement "return True" do? – 12johnny Nov 04 '15 at 09:01
  • 1
    @12johnny - If the for loop has completed checking divisibility through all the numbers (i.e. the `if` condition is always false for all the values of `i` in the loop. This means that the number is a prime number). Hence, it returns True if the execution completes the `for` loop. – JRodDynamite Nov 04 '15 at 09:08
  • @vaultah - Yes, I do know that, I've posted answers previously. I was editing and adding my explanation when you posted that comment. – JRodDynamite Nov 04 '15 at 09:17
  • @vaultah - If I had to post my answer along with the explanation, (which would've taken a bit longer) someone else could have answered it in the mean time. – JRodDynamite Nov 04 '15 at 09:20
  • So what? Are you saying that the [first revision of your answer](http://stackoverflow.com/revisions/c3100bbb-8e4f-4565-a107-c596847f96d0/view-source) is an acceptable and useful answer that actually provides the *answer* to this question? Then you're mistaken. – vaultah Nov 04 '15 at 09:24
  • 1
    @vaultah - Okay. Will include a small explanation next time I answer. And then include a more detailed explanation (if required) later. Thanks for advice! :) – JRodDynamite Nov 04 '15 at 10:39