Dont understand why my "is_prime" is not working


The error message:
Oops, try again. Your function fails on is_prime(0). It returns None when it should return False.

i clearly included a condition that return false if x < 2

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


Your loop always exits the function each iteration, so it isn't really a loop at all as it only iterates at most once


sorry i still don't understand


In order for the loop to run more than once, program control needs to reach the end of the loop and then go back up to the beginning and check the condition (which python does in the background as you're iterating over a sequence)

But inside your loop you have instructions for program control to leave the function altogether, therefore the aforementioned looping will not happen


Thanks. Makes sense now


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