FAQ: Learn Python - Practice Makes Perfect - factorial FAQ: Learn Python - Practice Makes Perfect - is_prime

faq
#1

This community-built FAQ covers the “s_prime” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise s_prime:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

#2

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!
split this topic #3

2 posts were split to a new topic: Why does this Return False When x = 9?

split this topic #4

2 posts were split to a new topic: Is prime - return indent

split this topic #5

2 posts were split to a new topic: Prevent the return False from breaking the loop

#6

hey bros I don’t like this I am pretty sure there is an additional step being made which is not necessary and shouldn’t be there.

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2, x-1): #I MEAN THIS LINE RIGHT HERE
            if x % n == 0:
                return False
        return True

print is_prime(13)
print is_prime(10)

Why is there the (-1) I don’t think it should be there. Or I am just tired and it still counts it from 0… like. Range(2)… 0, 1 and then stop at x-1 for instance 10 which would mean 9, numbers 2 to 9 that is… 1, 2, 3, 4, 5, 6, 7, 8, 9… oh god damnit I see now, somebody please confirm I understand. Wait no need to I get it.

#7

why does is_prime(3) equal none?

#8

and actually why it isnt working right?

#9

None means no value is returned. If x is greater then 2, the only possible return value is True. How should false be returned for values like 9 and 10 for example?

#10

I don’t understand the solution they have given:

def is_prime(x):
##if x < 2:
####return False
##else:
####for n in range(2, x-1):
######if x % n == 0:
########return False
####return True

For is_prime(2) it somehow gives True, yet 2 /2 =1, so 2 % 2 == 0, which should return False. What am I missing?
Also, in the range function why is it not range(2, x) as range functions don’t include the number after the comma? I assume no number divided by the previous number gives % == 0 (except possibly 2), but still it seems more certain to use x.

#11

i assume by so 2 % 2 == 0 you mean the if condition? Excepts the code never gets there, the for loop never makes any iteration, given the loop condition is false from the start

never understood that, range(2, x) works just as well.

#12

Great thanks, of course there is no n in range(2,1) or range(2,2), so it ignores the iteration. Thanks for your help.

#13

Why does the following code fail at -10?

def is_prime(x):
if x == 0 or x == 1:
return False
elif x == 2:
return True
else:
for number in range(2, x):
if x % number == 0:
return False
return True

#14

negative numbers can’t be prime numbers, so for negative numbers false must be returned

How do you think your code is currently handling that?

1 Like
#16

None indicate the absence of a return value, thus no return keyword in your function is reached. Why do you expect False?

1 Like
#18

I’m stuck. This is what I have:

def is_prime(x):
  if x < 2:
    return False
  for n in range(2, x - 1):
    if x % n == 0:
      return False
    return True
#19

Your function fails on is_prime(2). It returns None when it should return True.

None indicates the absence of a return value, so for x=2, we don’t get a return value. Which return value did you expect? And why doesn’t your code get there?

#20

Hi, why does is_prime(15) return True in the solution code, while 15 is not a prime number?

#21

Can’t replicate issue, using the solution code:

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2, x-1):
            if x % n == 0:
                return False
        return True

print is_prime(13)
print is_prime(10)
print is_prime(15)

i get false for 15, as expected

#22

what is false about my code:

def is_prime(x):
for e in x:
while e==range(2,x-1):
if e % n==0:
return False
else:
return True
print is_prime(17)
print is_prime(4)
print is_prime(1)