Is_prime


#1



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


All my error messages return is_prime(0) fails or is_prime(2) returns False when it should be true.


I can see why this happens. Due to my function x % n == 0. I have also tried (x / n) == 0.
Is my problem my function? or my whole program?


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


print is_prime(7)


#2

you should make a if condition to return False for numbers lower then 2

Then post an updated version of your code if you need more help


#3

I suggest adding print statements to make the program explain what it's doing, and then looking at that output to understand what it's doing (and obviously you'll need to call the function with some input that it's incorrect for)


#4

I thought the for statement would handle that. But would making an if statement solve my problem for is_prime(2)?


#5

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


print is_prime(7)

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


#6

True, your for loop handled it, but it also prevented you from ever successfully determine a prime number (as you will see when we get to the solution)

Add some print statement to see what is happening.

what is the logic of this:

if (x / n) % 2 == 0:

math?


#7

Attempts at correcting 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
    else: 
        return False

print is_prime(0)
print is_prime(7)


#8

you can also add print statements inside your function:

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

this will show you that for x=7 your loop makes only one iteration, not really what should happen


#9

Not sure I understand. When I add the print statement I am returned with

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

print is_prime(0)
print is_prime(7)

False
2
True
None

This raises more questions. Which is the print n answer?
Am I close or way off?


#10

2 is what n prints, but feel free to add a string:

print "looping for:", n

#11

I dont understand what youre trying to tell me. My code seems to work for everything except is_prime(2).
for looping: 2?
So my for loop only iterates once? My range is faulty?


#12

yep, in the first iteration of your loop, a return keyword is reached which ends the function (thus breaks the loop), for a number to be a prime number you need to return True after the loop has finished running, that is why you needed your if condition to check for values lesser then 2


#14

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