Is_prime(x)


#1

So this is my code to determine if a number (x) is prime or not. I tried to test my code by inputting 9 and 10 into the function, but nothing is happening. Also, the lesson says there is something wrong with my code and I looked at the answer code and I don’t think it is correct.

def is_prime(x):
    if x < 2:
        print False
    else:
        for n in range(2, x):
            if x % n == 2 or x%n==3:
              return False
        else:
          return True:
is_prime(9)
is_prime(10)

#2

If you simply call is_prime without a call to print, you just return the value, without actually displaying it. You also have a colon at the end of your second return statement:

def is_prime(x):
    if (x < 2):
        return False
    else:
        for n in range(2, x):
            if (x % n == 2 or x%n==3):
              return False
        else:
          return True

print(is_prime(9))
print(is_prime(10))

#4

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