Is Prime - issue with the number nine


#1



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


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

I thought I had this one down easy; but it will not work with nine (it returns true). My best guess if that that the x % n == 0 under my last if statement is running 9/2 != 0 therefore it's true... Do i need something above "return False" to iterate to other numbers in the final if statement? Thanks!


#2

Have it print out the steps it's doing, compare to the steps you think it should be doing


#3

The problem is your last else clause should be in line with the for loop

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

#4

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