Is_prime (with a list)


#1



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


No error. It just stops running, no reject or pass.


As I though it could pass, considering I following the instructions and came up with the n-list in which all numbers from 2 to x-1 are listed. With the for loop I go through this list in the modulo.

I though I was pretty cunning there, with the small details that it doesn't work :joy: :grinning:
Can somebody help me out, using as much of my Original code? (or explain me why it isn't the right approach).

Thanks a million!

S.


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


#2

i would recommend adding a function call:

print is_prime(9)

so you can see interpeter errors, which will show you that there is a problem at this line:

if x % number < 0

you miss a colon at the end of the line.

also, in the if condition, why did you choice for lesser then (<)? Could you explain?


#3

Good idea on the function call and the missing colon. I choose the < sign, because every smaller (so not included) 2 is not a prime.


#4

i was referring to this condition:

if x % number < 0

why do you use < here?


#5

I reversed the condition. So instead saying: if x % number == 0 is True. It stated if x % number < 0 is False.
(thinking of it now, reversing should actually be != but since there are all absolute/positive numbers it makes no difference).


#6

you can't do this, since then your loop still breaks in its first iteration? you really have to place return true outside/after the loop


#7

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

So if I implement your improvements, it still doesn't run. Any ideas how to runs this solution with a list or an explanation why a list isn't the suiting solution here?

Thanks!


#8

if the remainder equals zero the number is not a prime number, you are using the wrong comparison operator here

A function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the loop will break

so for x=9, the first condition we have is:

9 % 2 == 0

which is false, so else will run, which means a return keyword is reached, the loop breaks so the function can end

you really have to place return True outside/after the loop


#9

I appreciate your effort and time. Could you please copy the working code in here, as I sort of have trouble understanding everything. So I can compare it. Thanks a lot!


#10

i can't do that, its a violation of the guidelines:

def is_prime(x):
    n = list(range(2,x-1))
    if x <= 1:
        return False
    elif x == 2:
        return True
    else:
        for number in n:
            print number
            if x % int(number) == 0:
                return False
            else:
                return True
print is_prime(9)

see how for x=9, your loop only makes a single iteration? In order for the be a prime number, the loop needs to finish running and then after the loop return True, not in its first iteration (which causes loop to break and function to end)

also, range does not includes its stop value, so no need to also subtract one


#11

Those darn guidelines!

see how for x=9, your loop only makes a single iteration? In order for the be a prime number, the loop needs to finish running and then after the loop return True, not in its first iteration (which causes loop to break and function to end)

You mean that with x=9, the code finds that the first number from the list, which is 2, is True. And therefore breaks off the loop. Although I understand this, I have some trouble coming up with a solution.


#12

place return True outside/after the loop?

this way, if a number is not a prime number, at some point in the loop, return False will be reached, the loop will break, the function will end

when the number is a prime number, the whole loop finishes running, then its reaches return True


#13

def is_prime(x):
    n = list(range(2,x-1))
    if x <= 1:
        return False
    elif x == 2:
        return True
    else:
        for number in n:
            print number
            if x % int(number) == 0:
                return False
        return True
print is_prime(9)`Preformatted text`

Yes, it runs! It is alive! Great job :slight_smile: Thanks


#14

range doesn't include its stop value, so why also subtract one?

once you done that, you can get rid of this:

    elif x == 2:
        return True
    else:

make sure to indent your code properly again, for x=2 the loop won't run, but after wards, true is simply returned, so you don't have to make a special case for x=2.

and number should be integers, so no need to cast to integer (you can remove int())

just some optimization ideas


#15

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

Looks neat!


#16

you can even get rid of the else, if the value is one or less, return False will end the function, the loop will never be reached.


#17

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