Is_prime, why does my code return None?


#1



So apparently when this code checks for x % n, it returns None instead of False when x == 0. Why is that? Even in my elif when explicitly checking that x == 0 returns None instead of False inside my for loop.

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

However this code works; it returns False when x == 0:

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

Why does the first code not work yet the second code does?


#2

range has 3 arguments:

range(start,stop,step)

if you don't specify step, the default is +1. and 0 is smaller then 2, so the whole for loop will just be skipped


#3

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