Mean-variance test errors

I do not understand what these errors are nor how to fix the code. Two unsupported operand type for Nonetype and dict, and the error message.

calculations = {
    "mean": [],
    "variance": [],
    "standard deviation": [],
    "max": [],
    "min": [],
    "sum": [],
}
def calculate(n):
    if len(n) != 9:
        raise ValueError("List must contain nine members.")
        
    n = np.array(n).reshape((3,3))
    calculations["mean"] = [np.mean(n, axis = 1).tolist()\
        , np.mean(n, axis = 0).tolist(), np.mean(n).tolist()]
    calculations["variance"] = [np.var(n, axis = 1).tolist()\
        , np.var(n, axis = 0).tolist(), np.var(n).tolist()]
    calculations["standard deviation"] = [np.std(n, axis = 1).tolist()\
        , np.std(n, axis = 0).tolist(), np.mean(n).tolist()]
    calculations["max"] = [np.max(n, axis = 1).tolist()\
        , np.max(n, axis = 0).tolist(), np.max(n).tolist()]
    calculations["min"] = [np.min(n, axis = 1).tolist()\
        , np.min(n, axis = 0).tolist(), np.min(n).tolist()]
    calculations["sum"] = [np.sum(n, axis = 1).tolist()\
        , np.sum(n, axis = 0).tolist(), np.sum(n).tolist()]
    

    
    print(calculations)
    
n = [0,1,2,3,4,5,6,7,8]
calculate(n)

boilerplate-mean-variance-standard-deviation-calculator - Replit

{'mean': [[1.0, 4.0, 7.0], [3.0, 4.0, 5.0], 4.0], 'variance': [[0.6666666666666666, 0.6666666666666666, 0.6666666666666666], [6.0, 6.0, 6.0], 6.666666666666667], 'standard deviation': [[0.816496580927726, 0.816496580927726, 0.816496580927726], [2.449489742783178, 2.449489742783178, 2.449489742783178], 4.0], 'max': [[2, 5, 8], [6, 7, 8], 8], 'min': [[0, 3, 6], [0, 1, 2], 0], 'sum': [[3, 12, 21], [9, 12, 15], 36]}
{'mean': [[1.0, 4.0, 7.0], [3.0, 4.0, 5.0], 4.0], 'variance': [[0.6666666666666666, 0.6666666666666666, 0.6666666666666666], [6.0, 6.0, 6.0], 6.666666666666667], 'standard deviation': [[0.816496580927726, 0.816496580927726, 0.816496580927726], [2.449489742783178, 2.449489742783178, 2.449489742783178], 4.0], 'max': [[2, 5, 8], [6, 7, 8], 8], 'min': [[0, 3, 6], [0, 1, 2], 0], 'sum': [[3, 12, 21], [9, 12, 15], 36]}
None
{'mean': [[3.3333333333333335, 4.0, 4.333333333333333], [3.6666666666666665, 5.0, 3.0], 3.888888888888889], 'variance': [[3.555555555555556, 10.666666666666666, 6.222222222222221], [9.555555555555557, 0.6666666666666666, 8.666666666666666], 6.987654320987654], 'standard deviation': [[1.8856180831641267, 3.265986323710904, 2.494438257849294], [3.091206165165235, 0.816496580927726, 2.943920288775949], 3.888888888888889], 'max': [[6, 8, 7], [8, 6, 7], 8], 'min': [[2, 0, 1], [1, 4, 0], 0], 'sum': [[10, 12, 13], [11, 15, 9], 35]}
E{'mean': [[5.0, 3.0, 3.6666666666666665], [4.666666666666667, 4.333333333333333, 2.6666666666666665], 3.888888888888889], 'variance': [[10.666666666666666, 0.0, 14.888888888888891], [9.555555555555555, 11.555555555555557, 4.222222222222222], 9.209876543209875], 'standard deviation': [[3.265986323710904, 0.0, 3.8586123009300755], [3.0912061651652345, 3.39934634239519, 2.0548046676563256], 3.888888888888889], 'max': [[9, 3, 9], [9, 9, 5], 9], 'min': [[1, 3, 0], [2, 1, 0], 0], 'sum': [[15, 9, 11], [14, 13, 8], 35]}
EF
======================================================================
ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/test_module.py", line 10, in test_calculate
    self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[2,6,2,8,4,0,1,5,7]'")
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/unittest/case.py", line 943, in assertAlmostEqual
    diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'NoneType' and 'dict'

======================================================================
ERROR: test_calculate2 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/test_module.py", line 15, in test_calculate2
    self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[9,1,5,3,3,3,2,9,0]'")
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/unittest/case.py", line 943, in assertAlmostEqual
    diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'NoneType' and 'dict'

======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTests)
----------------------------------------------------------------------
ValueError: List must contain nine members.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator/test_module.py", line 18, in test_calculate_with_few_digits
    self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,])
AssertionError: "List must contain nine numbers." does not match "List must contain nine members."

----------------------------------------------------------------------
Ran 3 tests in 0.049s

FAILED (failures=1, errors=2)

I still have this question.

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