Is_prime I'm stuck and confused


#1


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

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

Why is it failing on argument(0) and am I doing this correctly?



#2

It asks
Define a function called is_prime that takes a number x as input.
For each number n from 2 to x - 1, test if x is evenly divisible by n.
If it is, return False.
If none of them are, then return True.
?


#3

Hi,

You use x % n = 0, when you should check if x % number == 0. n is the list you are iterating over.

Also look at this:


#4

Your code would be easier to read if you just did it like this:

for number in range(2,x-1):
    #...

#5

Thank you. I realized that I needed to deal with 0 and 1 before anything which made everything else a lot clearer!


#6

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