Is_prime


#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/learn-python/lessons/practice-makes-perfect/exercises/isprime?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>
Your function fails on is_prime(-10). It returns True when it should return False.

<What do you expect to happen instead?>

```python

Replace this line with your code.
def is_prime(x):
if x == 0 or x == 1:
return False
n = x -1
while n > 1:
if x % n == 0:
return False
n = n - 1
else:
return True

print is_prime(2)

<do not remove the three backticks above>

#2

here:

if x == 0 or x == 1:
   return False

why not simply check if x is lesser then 2? it saves a comparison, and negative numbers can’t be prime numbers, so you cover all the negative number, 0 and 1


#3

This is what I came up with and i think it seems to be correct. but codeacademy says Your function fails on is_prime(-10). what the ■■■■ !! Can a negative number be a sum or a prime number?


#4

:kissing_closed_eyes:thanks for the help! Much appreciated.


#5

This has nothing to do with codecademy or coding, this has to do with math:

negative numbers can’t be prime numbers


#6

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