6.is prime


#1





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


my code fails to check if 2 is prime or not.

I don't really follow what is n and what is x when python does the calculations. is n same as x as it is given in a range?

so e.g. for 3=x does that mean n is also 3?

Also why do the instructions tells us to do the range until x-1, if I put in x range formula already stops at x-1...


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


#2

n will hold all the values between 2 (2 included) and x (excluded)

a function ends the moment a return keyword is reached, this is currently happening in the first iteration of your loop, only return True after the loop has finished running

yes, so it is to the learner to realize that range does not include the stop value, in the instructions it says minus one because you don't want to include x, but range already does this for you


#3

thanks where do I read my code wrongly:

say I take my x as 9, I expect my formula to do the following

check if x is smaller than 2. it is not so it goes onto else statement.

on else statement it checks all the numbers between 2 and 8 if 9 can be divided by any of them.

it checks that 9 can be divided by 3 and says this argument is false


#4

here:

in the first iteration of the loop, there are two possibility's:

if x%n==0:

is true, or else will be executed

In both cases, a return keyword is reached in the first iteration of the loop, in which case the function (thus the loop) ends.

You only want to return True after the loop has finished running (checking all numbers)

so you want to have for/else rather then if/else


#5

def is_prime(x):
is_prime=False
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


#6

def is_prime(x):
    is_prime=False
    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

#7

that should work, does it?


#8

yes but I need to work a bit more on the difference between for else vs if else


#9

or you leave the else out all together (return True should then indented in the right way). This way, if a number is not a prime, at certain point in the loop, false will be returned, and the function (thus the loop) ends

if a return keyword is not reached in the loop, True will be returned (this in essence now also what is happening with for/else)


#10

some more questions:

it doesn't really matter whether I put the last return true at the same index as else or for. can you explain why this is so?

compared to previous code i pasted above i removed the first statement saying is_prime=False and my code still works. can i conclude that it was reduntant?

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

#11

I put the last return true at the same index ...

Sorry I meant indent of course.

apologies too many questions but I am experimenting what it does and what it does not.


#12

i just did:

this code:

    elif x==2:
        return True

is reduntant


#13

just to confirm my understanding:

elif x==2 is redundant because next argument range(2,x) (when x is 2) does give a strange range like (2,1) and goes to next return which is true?

many thanks by the way, this is so helpful.


#14

well, you can see this if you run the following code:

for i in range(2,2):
   print i
print "how many loops?"

see how many times i is printed? None

So it moves on to the next line, which in this case is another print, or in your case, a return True


#16

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