6. is_prime


#1



Sorry, where is my mistake.

error messages:
Oops, try again.
Your function fails on is_prime(9). It returns True when it should return False.


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


#2

Execute it in your head for x=9 and see which return statement you end up on. Then consider how you got there and what it should be doing differently.
Alternatively you can add print statements that explain what it does as it runs.


#3

Thank you. I found the problem:relaxed:


#4

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