1

Python newbie here, so bear with me...

Unfortunately there's no "support" for these tutorials, except posting questions in a Q&A forum and maybe another student can help. I know that there are a ton of Python prime functions out there, but I think I've come up with one that works. However, the Codeacademy interpreter doesn't like my solution.

Here is the challenge:

  1. Define a function called is_prime that takes a number x as input.
  2. For each number n from 2 to x - 1, test if x is evenly divisible by n.
  3. If it is, return False.
  4. If none of them are, then return True.

Here's my solution (yes, I know this is really non-Pythonic and super inelegant, but I'm learning):

def is_prime(x):
        x = int(x)
        if x > 0:
            return False
        if x == 0:
            return False
        if x == 1:
            print "1 is not a prime number"
            return False
        if x == 2:
            print "2 is a prime"
            return True
        for i in range(2, x):
        #print i
            if x % i == 0:
                print "this is not a prime number"
                return False
                break

        else:
            print "this is a prime number"
            return True

print is_prime(-10)

When I run the above in the Codeacademy interpreter, it's returning this error:

Oops, try again. Your function fails on is_prime(-10). It returns True when it should return False.

Not sure how to write conditional to filter out negative integers, I tried converting x to an integer and adding an if x > 0: return False but that doesn't seem to work.

What am I doing wrong here?

AdjunctProfessorFalcon
  • 1,790
  • 6
  • 26
  • 62

5 Answers5

2

You don't return the result for any value greater than 2.

For 0, 1 and 2, you do return it:

return True

For the fourth case that covers all other numbers, you only print the result, but you don't return it as a boolean value.

Edit: Your attempt to filter negative values fails, because you return False when the input is positive, not negative:

if x > 0: return False

You should use this instead:

if x < 0: return False
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
1
def is_prime(x):
    # All numbers < 2 are not prime
    if x < 2:
        return False

    for i in range(2, x):
        if x % i == 0:
            return False

    return True

print "is_prime({}): {}".format(-7, is_prime(-7))
print "is_prime({}): {}".format(-10, is_prime(-10))
print "is_prime({}): {}".format(11, is_prime(11))
print "is_prime({}): {}".format(18, is_prime(18))
Michael Qin
  • 629
  • 6
  • 10
0

First of all,

if x > 0:

needs to be

if x < 0:

Algebra says that > means greater than and < means less than. Because negative numbers are less than 0, you return False only when x < 0.

Then,

for i in range(2, x):
        #print i
        if x % i == 0:
            print "this is not a prime number"
            return False
            break


    else:
        print "this is a prime number"
        return True

Has a lot of indentation inaccuracies. The problem is, your else: statement is in conjunction with if x == 2: and not the if statement in the for loop. I, however, the all-knowing, understand your purpose and am here to save the day (sorry for that). Change all that stuff on the bottom to

for i in range(2, x):
    #print i
    if x % i == 0:
        print "This is not a prime number"
        return False
        #You don't need a break statement because you are returning a value.     This means that the function stops because it has completed its job, which is to give back something
print "this is a prime number"
return True
#If all that for looping was to no avail and the number is a prime number, we already know that by this point. We can just return True. 
rassa45
  • 3,482
  • 1
  • 29
  • 43
0

To cater for the none positive and zero use x <= 0

def is_prime(x):
x = int(x)
if x <= 0:
    return False
if x == 1:
    print "1 is not a prime number"
    return False
if x == 2:
    print "2 is a prime"
    return True
for i in range(2, x):
    #print i
    if x % i == 0:
        print "this is not a prime number"
        return False
        break
else:
    print "this is a prime number"
    return True
Glitch
  • 673
  • 8
  • 23
-1
def is_prime(x):
   if x < 2: 
       return False
   for n in range(2,x-1):
       if x % n == 0:
           return False
   else: 
       return True
Chris Edward
  • 101
  • 2