Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
"def isComposite(x):\n",
    "    \"\"\"Returns whether or not the given number x is composite.\n",
    "\n",
    "    A composite number has more than 2 factors.\n",
    "    A natural number greater than 1 that is not prime is called a composite number.\n",
    "    Note, the number 1 is neither prime nor composite.\n",
    "\n",
    "    For example:\n",
    "    - Calling isComposite(9) will return True\n",
    "    - Calling isComposite(22) will return True\n",
    "    - Calling isComposite(3) will return False\n",
    "    - Calling isComposite(41) will return False\n",
##########################
### TEST YOUR SOLUTION ###
##########################

composite_numbers = [4, 6, 8, 9, 10, 12, 14, 15, 16,
     18, 20, 21, 22, 24, 25, 26, 27,
     28, 30, 32, 33, 34, 35, 36,
     38, 39, 40, 42, 44, 45, 46, 48,
     49, 50, 51, 52, 54, 55, 56, 57,
     58, 60, 62, 63, 64, 65, 66, 91, 93]

for i in composite_numbers:
    assert_true(isComposite(i), str(i) + ' is composite')

not_composite_numbers = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23,
     29, 31, 37, 41, 43, 47, 53, 59,
     61, 67, 71, 73, 79, 83, 89]

for i in not_composite_numbers:
    assert_true(not(isComposite(i)), str(i) + ' is not composite')

#test existence of docstring
assert_true(len(isComposite.__doc__) > 1, "there is no docstring for isComposite")
print("Success!")


When i go to to test my solution i get an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-037cd1a71006> in <module>
21
22 #test existence of docstring
---> 23 assert_true(len(isComposite.__doc__) > 1, "there is no docstring for isComposite")
24 print("Success!")

TypeError: object of type 'NoneType' has no len()

What I have tried:

Python
def isComposite(x):
        # your code here
        if (x <= 1):
                return False
        if (x <=3):
                return False
        if (x % 2 == 0 or x % 3 == 0):
                return True
        i = 5
        while (i * i <= x):
                if (x % i == 0 or x % (i + 2) == 0):
                        return True
                i = i + 6
                return False
print(isComposite(9))
print(isComposite(22))
print(isComposite (3))
print(isComposite (41))


my output is:
true
true
false
false
Posted
Updated 2-Apr-22 21:37pm
v3

1 solution

You do not have a function called isComposite; all you have at the beginning of your source are a lot of strings. Take a look at 4. More Control Flow Tools — Python 3.9.12 documentation[^].
 
Share this answer
 

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