Is Prime


#1



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


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

I am really confused on what I'm supposed to do.


Replace this line with your code. 
def is_prime(x):
    if x==0 and x<2:
        return False
    elif x>=2:
        return True
    else:
        for n in range (2, x):
            if x%n==0 and x%1==n:
                return False
            else:
                return True


#2

nice code by the way
you need to take care of the condition wen passed 1.
remember that the for loop fails if 1 is passed so make sure that the for loop is entered only if x > 2 which you code does that, next is in you first if statement

try using or instead of and. remember that if statement is false even even 1 is entered
and use x == 2 for the elif

best of luck


#3

This is fuzzy logic. If x is zero, then it is less than 2. Try,

if x < 2:

That will cover both situations, as well as negative numbers, etc.

This will return True for all numbers that make it this far. The rest of the code will never run.

The else should not be inside the if statement. Either associate it with the for or remove else altogether and just write the return statement at the very end, outside of any constructs.


#5

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