Data Analysis with Python: broken tests in the first 2 exercises

Hi! I would like some help with the test in the Data Analysis with Python course exercises.

The Mean-Variance-Standard Deviation Calculator project fails the test cases because it cannot test if two lists in the dictionary are similar. As far as I can tell, the tests crash not because of my own code in the exercise. Stacktrace below:

Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/test_module.py", line 54, in test_calculate2
    self.assertAlmostEqual(
  File "/usr/lib/python3.8/unittest/case.py", line 937, in assertAlmostEqual
    if first == second:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I acutualy fixed the test myself by using a pandas testing function. But if the tests are indeed broken from the start, someone should fix them, before (more) people get confused. If the test function as expected, and i did something stupid; please let me know! :smiley:

The Demographic Data Analyzer test seem also broken. The values match more or less with the answers my code generates, but not close enough. I first thought my code was bad. However, after calculating the average male age manually in excel, I discovered my answer is actually correct, down to 7 decimals accurate. I’m not sure if the other failing test, fail because of faulty code of mine, or if the tests aren’t accurate enough.

Thanks for reading, and hopefully this can be resolved :slight_smile:

I might have faulty code in the first exercise. Need to look closer at the problem

Steps to reproduce the first problem don’t work:

Clone the Replit project from the exercise page

Hardcode the expected value of the first test into the answer of the calculate function:

def calculate(list):

    return {'mean': [[3.6666666666666665, 5.0, 3.0], [3.3333333333333335, 4.0, 4.333333333333333], 3.888888888888889], 'variance': [[9.555555555555557, 0.6666666666666666, 8.666666666666666], [3.555555555555556, 10.666666666666666, 6.222222222222221], 6.987654320987654], 'standard deviation': [[3.091206165165235, 0.816496580927726, 2.943920288775949], [1.8856180831641267, 3.265986323710904, 2.494438257849294], 2.6434171674156266], 'max': [[8, 6, 7], [6, 8, 7], 8], 'min': [[1, 4, 0], [2, 0, 1], 0], 'sum': [[11, 15, 9], [10, 12, 13], 35]}

run the program. The first test should still fail. It does not. I’m not sure why my own code does break this test. Maybe it has something to do with the less significant digits?

I’m officially lost:
Copying my own answer from the code into the test still yields the same error as above. I don’t understand what is going wrong. I’m pretty sure at least part of the test is faulty: the average of 1, 2 , and 8 is 3 2/3 and not 3.6666666666666665> I don’t know if digits that insignificant make a difference, but the fact it’s rounded down is weird either way

assertAlmostEqual is not able to determine how different two dicts are, but it is able to determine if two dicts are equal. Therefore the problem occurs when result is different from the expected one.

For the Demographic Data Analyzer, README.md file specifies to how many digits result should be rounded.

1 Like

Ah i’ve must have missed the rounding part. Will try that. Thanks for responding!

For the Mean-Variance-Standard Deviation Calculator consider also the following - function returns in dictionary numpy's arrays, not a standard python lists.

1 Like

I missed this part of the exercise:

The values in the returned dictionary should be lists and not Numpy arrays.

After converting my answer to lists the test worked as expected! Thanks for the help!

1 Like

Thank you for helping me! I should really learn to read… might help with future exercises :stuck_out_tongue:

Don’t be hard on yourself. Keeping track of all details can be hard, and often mind has natural tendency to overlook those little things, when it thinks it has all what is needed for a task.

2 Likes

Thank you for your kind words!

I’m aware language is not one of my strong suits (especially not in English). I don’t mind it though. You can’t win at everything. And fortunately there are other people to help if i get stuck.

Anyway, have a nice day!

it seems someone reported this some times ago

could you check this issue Problematic usage of assertAlmostEqual in Mean-Variance-Standard Deviation Calculator · Issue #39430 · freeCodeCamp/freeCodeCamp · GitHub

and add your comments if needed

Yeah of course! Happy to help :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.