Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Since, print returns NoneType so, NoneType will be compared with list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading