Is Prime returning none


I keep getting the error " Your function fails on is_prime(3). It returns None when it should return True."

not sure why it wouldn't return true or false for a given value

def is_prime(x):
    if x<2:
        return False
    elif x>2:
        for i in range(2,x):
            if x%i == 0:
                return False
        return True


as if some number does not qualify the elif case, It means it is a prime number and it should return True for that.
But here we can see that the if/elif/else statement used to connected and once one branch runs, once the condition fulfills for some case it does not care about any other branch of if/elif/else.

so It sees that function have nothing to return and by default it returns None


I don't understand your meaning, sir, respectfully. I believe the reason it returns None can only be for ONE REASON, based on his code. That it meets the elif condition BUT does NOT meet the if condition within it (OR it does not meet the range, which should be range(2, x - 1)). Therefore, it has already ran a 'branch' of your 'tree', and satisfied its parameter(s) so it does not continue forward, since it stops running once it has completed/fulfilled ANY ONE OF YOUR THREE CONDITIONS. In this case, the only one I can see that could result in None would be the elif (but x is either not within the range specified or not meeting the if condition). By the way, this won't solve the problem, but consider the second argument in the range and whether it needs to be modified based on the instructions..


If you want it to print a value (regardless of it being right or wrong) simply add an else to your if condition. If you want to solve the code/problem, however, I have another suggestion and am willing to share it but you need to request it by messaging me or by replying with an @ + myname so I get notified and understand your intentions. I don't want to spoil your fun and give you an answer if that's not what you want.. Cheers!


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