Is_prime


#1



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


I don't know whats wrong with my code, it said : "Oops, try again. Your function fails on is_prime(9). It returns True when it should return False"


Replace this line with your code. 

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


#2

Hi,

The issue is with your first else: statement. It should be indented differently :wink: I hope this helps.


#3

in line for n in range(2,x):
it should be x-1, not x
because a prime can be divided by itself and the parentheses for the function "range" is inclusive which is different from those in "list".


#4

I did what you said but it still false


it said "Oops, try again. Your function fails on is_prime(3). It returns None when it should return True."


#5

I did what you said but the console printed "Syntax Error"


#6

sorry, I missed your point.

it should be "x" not "x-1"
but the problem is on the first "else", the "else" should be deleted, and the indentation before the following return should be deleted.


#7

Okay, let's clear this up once and for all!

You are using range (2,x-1) instead of (2,x). In the original code you have posted this was correct :slight_smile:
How does range work? Range starts with the number you specify but it does not end with the second number. To illustrate, range(2,5) starts at 2 goes to 3 and finally 4. Therefore, you should not use x-1.

You can think of range as (starts at, stop one before this number).

Secondly, you left the indentation same for the else: statement (as in the original code you posted).

Thus, the code should look like this:

If it helps, the else: does not even need to be there :wink: as here (notice the indentation!):

Hopefully this helps :slight_smile:


#8

I agree with you with respect to the x instead of x-1 (x-1 is wrong) :slight_smile:

However, the else: statement is actually fine. Perhaps it is not the most "clean" way but it is totally fine. Please see my reply with the screenshots. You can actually give it a try with else: and you will see that it works :wink:

Btw, I like your approach of using x>3 and then the or x ==3. That is nice addition to it :slight_smile:


#9

Thank you for your compliments. With respect of the indentation of "else" and "return", I agree with you. But I am still confused about one thing. Could you explain to me why the appearance and disappearance of "else" here gives the same output.


#10

I found this article for the explanation of else: in loops.
The outcome will be the same because the 'else' is not required but optional.
What I understood from this was that It is used to make readable code.


#11

Sure :slight_smile: let me explain. Firstly, I referred to it as "less clean" because I was not sure whether it is covered by codeacademy or not. At that time, I did not want to dig into the topic deeper and perhaps confuse some people in the process.

That being said, the usage of "else" in combination with "for" statement is fine in Python. Now how exactly does it work?

Using the solution from before...we start with the "for" loop. If x is divisible by n then return true. Otherwise (this is the else), return True. In other words, "find some item in the for loop, else if none was found do ..."

If you are still curious check answers (someone had similar question as you) here: http://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops
(P.S.: stackoverflow is quite useful site for coding related problems/answers).


#12

You can use this (it works):

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

#13

Hi guys, I've read your discussion, but I still don't understand why the code doesn't work particularly for x = 9.
I have the same problem, I've tried this code in a python terminal, tested manually it for many different numbers and it seems not to work for x = 9 only. Why would that be?
The code returns "True" for x = 9 just as if it completely skipped the for loop. Thanks for your help.


#14

I'm not sure why it makes it fail, but I was able to finish it using a solution almost identical to the one you came to as well. The only difference was I kept the "return true" you have in the for statement on the same indent as the "for" statement so it read like so (I'm sorry I'm new to the comments so I don't know exactly how to format this):

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

For some reason it works if you return True after the for loop has finished running, and I'd love if someone could please explain why. I understand that the return command works like a break so when the if statement returns False the True after the loop would never run, I just don't understand why it has to be outside of the loop to work, but if it's in an else statement in the loop it provides false positives.


#15

Of course, you're right, it makes perfect sense.
Now I actually understand why it wouldn't work for multiples of 3. In my code the loop only ever gets to i = 2, because if x % 2 =/= 0 the code goes straight to the else: statement and breaks the loop.
Therefore, my code would show that all even numbers aren't prime numbers, but any uneven number is.
In other words, the code never gets to run x % 3 == 0, because if it fails on x % 2 == 0, breaks the loop and returns True.

Thanks for your help, very insightful indeed! :slight_smile:


#16

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