Is_prime returns None


#1


HI All,
my code throws None at x=3. In my view, x = 3 should be covered in the for loop, and thus return True.
I get none instead. Why is it not looking at the else statement?

Thanks for your help in advance!


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

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


#2

well this statement is true for 3:

if x > 2:

once if is true, elif and else won't run.

then your loop:

for n in range (2, x-1):

why x-1 as stop value? range won't include the stop value (x), so why also subtract one?


#3

Thanks! I changed the stop value.

What I still don't get is that even if x>2 is a true statement (since x = 3), it should still go thru the for loop, and only stop when reaching the else within the for loop.

if x > 2:
        for n in range (2, x):
            if x % n == 0:
                return False
            else: return True

So it should stop here and return "True"


#4

Because that is what is given in the instructions. We don't know if the author meant literally, or theoretically. At any length, it is moot since either value is well above the needed range:

range(2, int(x ** 0.5) + 1))

Demo:

>>> a = [(x, int(x ** 0.5) + 1) for x in range(15,300,2)]
>>> a
[(15, 4), (17, 5), (19, 5), (21, 5), (23, 5), (25, 6), (27, 6), (29, 6), (31, 6), (33, 6), (35, 6), (37, 7), (39, 7), (41, 7), (43, 7), (45, 7), (47, 7), (49, 8), (51, 8), (53, 8), (55, 8), (57, 8), (59, 8), (61, 8), (63, 8), (65, 9), (67, 9), (69, 9), (71, 9), (73, 9), (75, 9), (77, 9), (79, 9), (81, 10), (83, 10), (85, 10), (87, 10), (89, 10), (91, 10), (93, 10), (95, 10), (97, 10), (99, 10), (101, 11), (103, 11), (105, 11), (107, 11), (109, 11), (111, 11), (113, 11), (115, 11), (117, 11), (119, 11), (121, 12), (123, 12), (125, 12), (127, 12), (129, 12), (131, 12), (133, 12), (135, 12), (137, 12), (139, 12), (141, 12), (143, 12), (145, 13), (147, 13), (149, 13), (151, 13), (153, 13), (155, 13), (157, 13), (159, 13), (161, 13), (163, 13), (165, 13), (167, 13), (169, 14), (171, 14), (173, 14), (175, 14), (177, 14), (179, 14), (181, 14), (183, 14), (185, 14), (187, 14), (189, 14), (191, 14), (193, 14), (195, 14), (197, 15), (199, 15), (201, 15), (203, 15), (205, 15), (207, 15), (209, 15), (211, 15), (213, 15), (215, 15), (217, 15), (219, 15), (221, 15), (223, 15), (225, 16), (227, 16), (229, 16), (231, 16), (233, 16), (235, 16), (237, 16), (239, 16), (241, 16), (243, 16), (245, 16), (247, 16), (249, 16), (251, 16), (253, 16), (255, 16), (257, 17), (259, 17), (261, 17), (263, 17), (265, 17), (267, 17), (269, 17), (271, 17), (273, 17), (275, 17), (277, 17), (279, 17), (281, 17), (283, 17), (285, 17), (287, 17), (289, 18), (291, 18), (293, 18), (295, 18), (297, 18), (299, 18)]
>>>

#5

else is reached in the first run of the loop, if you want else only to be reached after the for loop change if/else to for/else (hint: indention)


#6

You are a genius!! Thanks! I changed the indention and it worked!


#7