Is_prime(3) returns False


#1

<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.>
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#

<In what way does your code behave incorrectly? Include ALL error messages.>
is_prime(3) reports false instead of true

<What do you expect to happen instead?>
The code is pretty straightforward, I don’t see the problem.
can anyone spot the error?

```python

def is_prime(x):
if x < 2: # 1 or less is automatically false
return False
for num in range(2, x): # go through each number
if x % num == 0: # divisible by num, so not prime
break
return False
else:
return True.

<do not remove the three backticks above>

#2

please format your properly then we can help you better.

now you can do

  • Edit your post
  • select the whole code
  • click on the </> above

now i can only say that you shouldn’t use the break statement in your code. It is because then it’ll return none.


#3

It automatically un-formats it.
here is a snapshot:

Capture


#4

why did you use break statement there?It can’t reach to return statement.

remove the break and use return statement for if


#5

Wrap your code block in back ticks, 3 before the block, and 3 after the block. This will preserve your formatting.

Your range should not go all the way to x because then it will always return false (x % x is 0). (hint, have your maximum in your range equal to 1 less than x).

what is the pupose of your break statement?


#6

removing break fixed it.
thanks guys.


#7

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