Dont understand why my "is_prime" is not working


#1



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


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
        else:
            return True


#2

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


#3

sorry i still don't understand


#4

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


#5

Thanks. Makes sense now


#6

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