Is_prime


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Your function fails on is_prime(3). It returns None when it should return True.


i have been struggling through most of practice section but i was almost sure this was right. it makes sense to me if i read it out loud would like to see if someone could also explain why i am off and how close was it to a good answer


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


#2

in your function, it is pretty difficult for a prime number to be validated as a prime number. only 2 is a prime number.

the problem is here:

if x > 2:
   # rest of your code
else:
   return True

so true will only be returned if the number isn't larger then 2, except you already have this covered with your other if statements.

Maybe else should be indented differently?


#3

so should i take x >2 out since i have if x >2: return false?

I have tried indenting it on the outside and inside of the loop but got the same respose except it stops at (9) instead of three.


#4

Consider this example of for...else:

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

If the loop completes, the code in the else block runs, otherwise the next line runs (break skips the else block). The last line only runs when x == 2. Notice that the else is paired up with for not if.

I've only given this code as an example, not a solution, even though it does work. With a small change we can write this without an else block.

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

Notice we don't have to check if x == 2 in either example. That state falls right through the function to the final return statement.


#5

Olá, observe o que o laço for faz dentro da função ( is_prime ) :smile:

Defina uma função chamada is_prime que toma um número x como entrada
Para cada número n de 2 a x - 1, teste se x é divisível por n.
Se for, retorne False.
Se nenhum deles for primo, retorne True.

def is_prime(x):
if x < 2:
return False
elif x == 2:
return True
else:
for n in range(2, x - 1): / # Para cada número n de 2 a x - 1
if x % n == 0: / # se x é divisível por n.
return False / # retorne False.
else:
return True / # Se nenhum deles for primo, retorne True.


#6

yes, but range doesn't include x, so no need to x-1 again in range

Your indent doesn't show on the forum, read the help section on the forum, it also contains a topic about markup to make indent show

Given this is the english track, it would be easier if help was in english as well.


#7

for n in range(2, x):

É verdade, desse modo não é necessário repetir ( -1).


#8

Thank you I felt really lost for a while there.

just to make sure that i understand what is going on.

i had two main problem.
i had the else statement stopping the if x % n==0.
and then i forgot to ask for a return even if it did run correctly

is that correct?


#9

The placement of the else was the issue. When the loop completed it did not see the return statement. Something to consider about testing for 2... If x == 2 then return True does not need to be followed by another if. It's not less than or equal to 2, so it must be greater. That's a given.

def is_prime(x):
    if x < 2:
        return False
    if x == 2:
        return True
    for n in range(2,x):
        if x % n == 0:
            return False
    # this runs after the loop
    return True

#10

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