-1

This is my code:

def chickenpox_by_sex():
    import pandas as pd
    df= pd.read_csv('assets/NISPUF17.csv')
    df=df[df['P_NUMVRC']==1]
    a=df[df['HAD_CPOX']==1]
    b=df[df['HAD_CPOX']==2]
    m_flu=len(a[a['SEX']==1])
    m_noflu=len(b[b['SEX']==1])
    f_flu=len(a[a['SEX']==2])
    f_noflu=len(b[b['SEX']==2])
    
    dict={"male":m_flu/m_noflu,"female":f_flu/f_noflu}
    print (dict)
chickenpox_by_sex()

And here is the error that I receive:

TypeError                                 Traceback (most recent call last)
    <ipython-input-6-3c57eb17d825> in <module>
    ----> 1 assert len(chickenpox_by_sex())==2, "Return a dictionary with two items, the first for males and the second for females."
          2 

Can anyone shed some light on the situation?

fdermishin
  • 3,519
  • 3
  • 24
  • 45
kwartz
  • 1
  • 1
  • What line is this error happening on? – wamster Jan 10 '21 at 16:46
  • 2
    You should post a bit more information about your dataframe. there is now way we can guess what's the content of your csv file. Post at least a sample of it as well as expected result/output. – Danail Petrov Jan 10 '21 at 16:51
  • Avoid importing inside a function, see https://stackoverflow.com/questions/128478/should-import-statements-always-be-at-the-top-of-a-module – fdermishin Jan 11 '21 at 03:58
  • its only that your function is not returning anything so it's type will be None try "type(chickenpox_by_sex())" so it won't have any len or any function. Moreover pay attention to few ore things 1. do not import inside function as mentioned by fdermishin 2. dict is reserved keyword don't overwriting it can coz a lot of trouble as the modules you have imported & and you yourself might need to use it somewhere 3. as you are dividing by some variable in dictionary the value can be zero so you might encounter division by zero – vBrail Jan 11 '21 at 04:13
  • Will keep all of this in mind when in future asking questions. :) – kwartz Jan 11 '21 at 18:24

2 Answers2

0

Your function chickenpox_by_sex() does not return anything that would have a length of 2. In fact it returns None. This equivalent to:

>>> assert len(None)==2, "Return a dictionary with two items, the first for males and the second for females."
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'NoneType' has no len()

which fails even before the assertion is tested.

I think you should be returning the dictionary from the function:

result = {"male" : m_flu/m_noflu, "female" : f_flu/f_noflu}
print(result)
return result

if you want to print it before returning from the function. Or you can simply return a dict:

return {"male" : m_flu/m_noflu, "female" : f_flu/f_noflu}

Then:

>>> assert len({"male" : 1, "female" : 2})==2

will pass.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Thanks :) I have made the necessary adjustments and get this error; if (chickenpox_by_sex()!=answer_chickenpox_by_sex()): raise AssertionError – kwartz Jan 11 '21 at 18:26
  • No problem. I don't know what your new problem is. Why is that an error? – mhawke Jan 11 '21 at 22:55
0
import pandas as pd
import numpy as np

def chickenpox_by_sex():
    df = pd.read_csv('assets/NISPUF17.csv', index_col=0)

    cpo_sex = df[df['P_NUMVRC'].gt(0) & df['HAD_CPOX'].lt(3)].loc[:,['HAD_CPOX','SEX']]
    
    print(cpo_sex)

    one_male = (cpo_sex[(cpo_sex['HAD_CPOX']== 1) & (cpo_sex['SEX']== 1)]).size
    one_female = (cpo_sex[(cpo_sex['HAD_CPOX']== 1) & (cpo_sex['SEX']== 2)]).size

    more_male = (cpo_sex[(cpo_sex['HAD_CPOX'] == 2) & (cpo_sex['SEX']== 1)]).size
    more_female = (cpo_sex[(cpo_sex['HAD_CPOX'] == 2) & (cpo_sex['SEX']== 2)]).size

    chickenpox_by_sex = {'male': one_male/more_male,
                         'female': one_female/more_female}
    print(chickenpox_by_sex)

    return chickenpox_by_sex
Ank
  • 1,704
  • 9
  • 14