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

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!

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!

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

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

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

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.

2 Likes

3 posts were split to a new topic: Why doesn’t this work?

3 posts were split to a new topic: Why is n=2 returning true?

2 posts were split to a new topic: Why doesn’t -10 work?

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

1 Like

2 posts were split to a new topic: Why does this fail on x=2?

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

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

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)

If x is a number, it is not iterable. Consider, do we need nested loops to solve this problem?

6 posts were split to a new topic: How to utilize a loop of for and else?

3 posts were split to a new topic: Do I need break or return?

2 posts were split to a new topic: Not able to understand the following program

def is_prime(x):
if x == 2:
return True
if x < 2:
return False
else:
j = 0
for n in range (2, x):
if x % n == 0:
return False
return True
print is_prime(2)
print is_prime(3)
print is_prime(4)
print is_prime(5)
print is_prime(6)
print is_prime(7)
print is_prime(8)
print is_prime(9)

Hi, I was wondering if someone could help me solve my code’s problem with the 9 (the print statements are only to verify answers as you can see). It doesn’t count 9 as a prime and I was wondering why. lately, I have been troubled by multiples and 9 appears to persist somehow.

Not sure what to make of this statement. Do we even need an else clause?