Click here to Skip to main content
15,120,268 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23,
     29, 31, 37, 41, 43, 47, 53, 59,
     61, 67, 71, 73, 79, 83, 89, 97,
     101, 103, 107, 109, 113, 127,
     131, 137, 139, 149, 151, 157,
     163, 167, 173, 179, 181]

for i in prime_numbers:
    assert_true(isPrime(i), str(i) + ' is prime')

not_prime_numbers = [1, 8, 12, 18, 20, 27, 28, 30,
     42, 44, 45, 50, 52, 63, 66,
     68, 70, 75, 76, 78, 92, 98,
     99, 102, 138, 148, 150, 156, 158]

for i in not_prime_numbers:
    assert_true(not(isPrime(i)), str(i) + ' is not prime')

#test existence of docstring
assert_true(len(isPrime.__doc__) > 1, "there is no docstring for isPrime")
print("Success!")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-28-29884f8bc4fb> in <module>
     11 
     12 for i in prime_numbers:
---> 13     assert_true(isPrime(i), str(i) + ' is prime')
     14 
     15 not_prime_numbers = [1, 8, 12, 18, 20, 27, 28, 30,

NameError: name 'assert_true' is not defined


What I have tried:

def isPrime(x):
    """Returns whether or not the given number x is prime.

    A prime number is a natural number greater than 1 that cannot be formed
    by multiplying two smaller natural numbers.

    For example:
    - Calling isPrime(11) will return True
    - Calling isPrime(71) will return True
    - Calling isPrime(12) will return False
    - Calling isPrime(76) will return False
    """
    
    # your code here
   
    if num>1:
        s=int(num/2)
        for i in range(2,s+1):
            if num%i==0:
                return("not Prime")
                break
        return("Prime")
print(isPrime(239)) 
Posted
Comments
CHill60 22-Oct-21 5:44am
   
You need a line at the top to import the function
from pyinq.asserts import assert_true

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900