-1

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

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
JVR
  • 23
  • 4
  • What are you trying to do here? Do you want to check if a tuple of a certain list equals to a value? Please clarify it. –  Nov 02 '22 at 00:44
  • [*Please do not post text as images*](https://meta.stackoverflow.com/q/285551). Copy and paste the text into your question and use the code formatting tool (`{}` button) to format it correctly. Images are not searchable, cannot be interpreted by screen readers for those with visual impairments, and cannot be copied for testing and debugging purposes. Use the [edit] link to modify your question. – MattDMo Nov 02 '22 at 00:48
  • `type(div_3(data_list))` is looking at the type of the return value of `div_3(data_list)`. However that function does not contain any `return` statements, therefore it returns `None` by default. (The function _prints_ a list, but that is not the same as _returning_ a list.) – John Gordon Nov 02 '22 at 01:29

1 Answers1

1

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

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

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • 1) You can omit the `()` in `return`. 2) It's not so much about `print` returning `None`, it's that the function doesn't return anything (and thus `None`) if it has no `return` statement… – deceze Nov 02 '22 at 00:56
  • `()` omitted from return – Talha Tayyab Nov 02 '22 at 00:58