Why is there an error asserting that output is a list?

Question:

aim is to write a function div_3() that will find all the values from the input parameter data_list that are perfectly divisible by 3, using a for loop with a conditional inside the function. And to save any values from data_list that meet this condition into a new list ‘output’, which is returned from the function.

data_list = [12, 49, 67, 308, 23, 15, 36, 21, 410]

def div_3(data_list):
   
    output = []
    
    for i in data_list:
        if i % 3 == 0:
            output.append(i)
        else:
            continue
    print (output)

test:

div_3(data_list)

[12, 15, 36, 21]

assert type(div_3(data_list)) == list

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/tmp/ipykernel_529/3718698975.py in <module>
      1 div_3(data_list)
      2 
----> 3 assert type(div_3(data_list)) == list

AssertionError:

I’m not sure why the output prints as a list [], but when trying to assert type == list, it produces an error

Asked By: JVR

||

Answers:

instead of print(output) ,return output. Your error will go away

Since, function does not return anything so, NoneType will be compared with list.

Answered By: God Is One
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.