<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
is_prime(9) returns True, should be False.
<What do you expect to happen instead?>```python
Replace this line with your code.
def is_prime(x): if x > 3: for n in range(2,x): if x % n == 0: return False break else: return True elif x == 2 or x == 3: return True else: return False print is_prime(9)
There’s been some issues with the forum displaying different indentations for the code.
The logic behind this is that it first checks if x > 3. Assuming that’s true, for each number n in [2, x-1] it checks whether x is perfectly divisible by n.
In the case of is_prime(9), first 9 > 3 so for each number n in [2, 8] it should test whether x % n = 0. Since the number 3 falls within this range, I would expect the function to return False yet it gives True for some reason.
edit: Figured it out! Seems like when the first n returns True it stays that way and stops the loop.
Changed the indents:
def is_prime(x): if x > 3: for n in range(2,x): if x % n == 0: return False else: return True elif x == 2 or x == 3: return True else: return False print is_prime(9)
How do I delete the post?