6. is_prime



I am putting in the code, but it thinks that 2 is not included in x >= 2

I was expecting for 2 to be put in the if statement, but it doesn't seem to

#My Code
def is_prime(x):
(tab)if x >= 2:
(tab)(tab)for n in range(2, x-1):
(tab)(tab)(tab)if x % n == 0:
(tab)(tab)(tab)(tab)return False
(tab)(tab)(tab)(tab)return True
(tab)(tab)(tab)elif x == 2: #Used in an attempt to solve the issue
(tab)(tab)(tab)(tab)return True
(tab)(tab)(tab)(tab)return False


I have an error in the post, but the extra tabs after the return True statement aren't in the code (the else and elif statements from then on are part of the original if statement), I still can't find the error, though.


Do you mean that it's failing for is_prime(2)? Your first condition is satisfied for that input. I suggest adding prints to find out what it does (don't guess). For example you can put a print statement as the first thing it does after testing that condition to verify whether it is satisfied for some input, or not.


I have. It is counting as a failure for is_prime(2)


Just finished. Don't need to respond anymore


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