Data Analysis with Python Projects - Demographic Data Analyzer

Tell us what’s happening:

Hi all,
when I try to run a test of the above project I get this error :

ERROR: test_higher_education_rich (test_module.DemographicAnalyzerTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-demographic-data-analyzer/test_module.py”, line 27, in test_higher_education_rich
self.assertAlmostEqual(actual, expected, msg=“Expected different value for percentage with higher education that earn >50K.”)
File “/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/unittest/case.py”, line 876, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘float’

This question has already been asked twice in here, however, none of the answers help me. Any idea? Running the code in Jupyter notebook raises no error and returns the expected results. Thanks

Your code so far

filter_advanced = [“Bachelors”, “Masters”, “Doctorate”]
advanced_ed = df[“education”].isin(filter_advanced)
more50k = df[“salary”] == “>50K”
h = len(df[advanced_ed & more50k]) / len(df[advanced_ed]) * 100
higher_education_rich = round(h, ndigits=1)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Data Analysis with Python Projects - Demographic Data Analyzer

Can you also send a link to the replit so we can see the code?

Hi,
sure, here it is:

The important part of the error is:
TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘float’.
Which means that somewhere the code is trying to take a “NoneTyoe” (None aka nothing) and try to substract a float of this.

To figure out where this happens we can look at the line they give us:
File “/home/runner/boilerplate-demographic-data-analyzer/test_module.py”, line 27, in test_higher_education_rich
So in line 27 of test_module.py you can see the following code:

self.assertAlmostEqual(actual, expected, msg="Expected different value for percentage with higher education that earn >50K.")

The expected part you can see is assigned on line 26 as expected = 46.5 (which is a float).
The actual part is what you have defined, which they try to access in line 25 actual = self.data['higher_education_rich'].
Which the error is telling us is None.

So if we look at your code to figure out why it is None we can see that you most definitly gave it data on line 26 demographic_data_analyzer.py. But then in line 41 of demographic_data_analyzer.py it’s set back to None.

(The same thing happened to lower_education_rich).

So if you remove those None assignements of the variables/data you already figured out you should be able to pass those sections.

I think they originally intended you to replace the var = None with the actual code to get the data.

2 Likes

I see it now!
HungryBee, thank you very much for your help. I definetely did not expect to look at this place, I was looking into my code where the “none” took place.
Thank you once again!

2 Likes