Is_prime question - I passed but the code is obviously wrong


#1

The compiler says I passed, but i tested it with some numbers.

For 12, I get no output
For 9 the output is: The number is not prime
For 2, I get no output
For 5: I get the wrong output 2 times:
The number is not prime
The number is not prime

This is the link:

This is the code:

def is_prime(x):
  if x >= 2:
    for n in range(2, x-1):
      if x%n == 0:
        return False
      print "The number is not prime"
    else:
      return True
    print "The number is prime"
  else:
    return False
  print "The number must be greater than 2"

is_prime(5)

How can the compiler pas me when the prints are not working correctly?


#2

your function returns the correct results for the numbers, which you can print by putting a print statement before the function call

however, your print statements within the function do not correspond with the values returned by the function.

for example, to make the print statements within the function correct you would need change this:

        return False
      print "The number is not prime"

to:

        return False
        print "The number is not prime"

but as you might have figured out, this won’t work either. return is the last thing a function does, so code after a return keyword isn’t reached. You would need to swap the return and print lines


#3

THANK YOU! I really appreciate and will keep going now.