11
votes

This is a question from codeacademy.com, where I am learning Python. So what I want is to define a function that checks if a number is prime. If it is, return True. If it isn't, return False.

Here is my code:

def is_prime(x):
    lst = []       # empty list to put strings 'False' and 'True'

    for i in range(2,x): # starting at 2 and not including x (number 1 is a divisor of all numbers

        if x <= 2:           # [1] see bellow the explanation
            lst.append('False')
            break

        elif x % i == 0: # if x is divisible by i(number between 2 and not including x)
            lst.append('False')
            break        # break, because we already know x is not prime

        elif x % i > 0:
            lst.append('True') # x is not divisible by i

    if 'False' in lst:
        return False     #  x is not prime - return False

    else:
        return True  # 'True' is in lst, so x is prime - return True

print is_prime(-2) # [2] I get an error here. See below

[1] - I made this condition because in codeacademy it says: "Hint Remember: all numbers less than 2 are not prime numbers!"

[2] - When I run, for example, 'print is_prime(11)' or 'is_prime(6)' it works ok. So I submit the answer, but codeacademy doesn't accept it. It says: "Your function fails on is_prime(-2). It returns True when it should return False."

3
Hi and welcome to StackOverflow. Actually, this is a very good question, and you have provided all the information necessary to reproduce your problem - this is very rare for StackOverflow first-posters. So congratulations on that (and on your English as well). Glad to have you here! - Tim Pietzcker
I highly recommend pythontutor.com for visualizing execution: if you go there, and paste your code into the execution window, you'll see why you get True for is_prime(-2). - Mark Dickinson
Besides that, you don't need to check all the numbers between 2 and x if you want to find prime numbers - int(math.sqrt(x)) is a sufficiently high upper boundary. - Tim Pietzcker
Just +1 the question for the same thing as @TimPietzcker said... If everyone on SO could be like that, at your age... :) - Maxime Lorant
Thank you everyone! You have really motivated me to keep going on this awesome world of programming... - vicaashow

3 Answers

11
votes

Let's see what happens when you enter -2:

  • range(2,-2) is empty, so the for loop never runs.
  • Therefore, lst is still [] after the loop.
  • Therefore, 'False' in lst is False
  • Therefore, return True is executed.
4
votes

When x is -2, range(2, x) will produce an empty list.

print range(2, -2) # will print []

So, the loop and the if conditions inside the loop will not be executed. The last if condition will be checked and no 'False' is in the lst. So, it returns True.

You can write the same program like this

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

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