Need help with is_prime


#1



Practice makes perfect: is_prime

The error message it displays is " Your code threw a "int() argument must be a string or a number, not 'list'" error."

This lesson has been particularly confusing for me and I just can't figure out how to go about solving this problem. I know that for the current code I have it won't work due to the fact that n is still a list. However I have tried to convert it into an integer with no success, which leads me to believe that there is some other solution. I have been working on this one problem for some time now and would like some insight. I don't want the answer I just want to understand how my current logic is flawed and how (if it is possible/helpful) to change a list into an integer or why that (changing the range to an integer) is not a way to solve this problem.


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


#2

Remove the else clause from the if statement.


#3

Hey there! You're very close, try using a for-loop instead of n = (range(2, x - 1).
The modulus operator only works for two integers (int) and n is a range between 2 and x - 1, thus a collection.


#4

So, I change my code so that I now have, "For n in range(x, 2-1)" and I added code to deal with 0 and 1, but it is now saying that is_prime(3) is returning None instead of True.

Also in response to mtf, which else statement should I remove?


#5

If it returns none, the value 3 must somehow have slipt throught the if-statements. Could you post the updated code?


#6

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

#7

Could you format your code like in your original post so we can see what goes where?


#8


#9

Try this, if it works, I will explain it.

def is_prime(x):
	if x == 0 
            return False
        if x == 1:
            return False
	if x > 2:
	    for n in range(2, x - 1):
		if x % n == 0:
		    return False
		else:                          #  <- remove this else
		    return True            # <- move this back so it executes after the for-loop

#10
  1. Remove the last else statement.
  2. Replace

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

with

if x == 1 or x == 0 or x < 0: #It's to account for negatives and special cases in one go
    return False

3.Change if x > 2 to if x >= 2 As you still need to include 2 somewhere in your code
4.Unindent your else statement in your loop so it's in the same indent as your for statement and your good to go!


#11

           for n in range(2, x - 1):
                if x % n == 0:
                    return False
                else:                 # remove
                    return True       # this one

#12

Thanks so much for the help! :smiley:


#13

Why does removing the else from the for loop and executing return true after the for loop work?


#14

That's because we want to loop over every number in range(2, x - 1) and check if it is evenly dividable by the number x.
This is checked by the if-statement inside the loop, False is returned if the number in question is evenly dividable by x. But if the number is not evenly dividable by x, we don't want to return anything and move on to the next number.
Only if we've looped over every number in range(2, x - 1), we want to return true.

With the else-statement the loop will quit too early and return True when it possibly should return False..


#15

The minus one part is not needed.

for x in range(2, x):

will work fine. It is moot, though when we consider,

a. A number is not divisible by any number greater than half its value.
b. A number with two factors will have one below the square root, and one above.

These considerations are not applied in this exercise but bear mention.

Consider,

def is_prime(x):
    if x < 2 or x % 2 == 0 and x != 2: return False
    elif x > 2:
        a = 3
        while a <= x ** 0.5:
            if x % a == 0:
                return False
            a += 1
    return True

#16

I know range(2, x) will work fine, but if you look at his code and the instructions of the excersise....


#17

That's just it. The upper bound of range() is x - 1. We don't need to write it in the actual parameters, then it would be x - 2.


#19