6. is_prime


#1



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


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)else:
(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)else:
(tab)(tab)(tab)(tab)return False


#2

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.


#3

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.


#4

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


#5

Just finished. Don't need to respond anymore


#6

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