6. is_prime


Where am I going wrong here?

Oops, try again. Does your is_prime function take exactly one argument (an integer)? Your code threw a “‘int’ object is not iterable” error.

def is_prime(x):
    n = x-1
    for i in n:
        if x < 2 or i % n == 0:
            return True
            return False. 

6. is_prime (Should be right but isn't)

for i in n:, n is a integer, integers can’t be looped over. This would require using range()

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

This gives the error message.

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

I thought that the for loop should handle 2.


i think you swapped true and false, if x is divisble by any number in range (n), its not a prime number, so false should be returned.


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