Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

Tell us what’s happening:

My code looks correct, I have checked that on my jupyter lab, but in replit it keeps throwing this error

{'mean': [[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0], 'variance': [[6.0, 6.0, 6.0], [0.6666666666666666, 0.6666666666666666, 0.6666666666666666], 6.666666666666667], 'standard deviation': [[2.449489742783178, 2.449489742783178, 2.449489742783178], [0.816496580927726, 0.816496580927726, 0.816496580927726], 2.581988897471611], 'maximum': [[6, 7, 8], [2, 5, 8], 8], 'minimum': [[0, 1, 2], [0, 3, 6], 0], 'sum': [[9, 12, 15], [3, 12, 21], 36]}
{'mean': [[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0], 'variance': [[6.0, 6.0, 6.0], [0.6666666666666666, 0.6666666666666666, 0.6666666666666666], 6.666666666666667], 'standard deviation': [[2.449489742783178, 2.449489742783178, 2.449489742783178], [0.816496580927726, 0.816496580927726, 0.816496580927726], 2.581988897471611], 'maximum': [[6, 7, 8], [2, 5, 8], 8], 'minimum': [[0, 1, 2], [0, 3, 6], 0], 'sum': [[9, 12, 15], [3, 12, 21], 36]}
EEF
======================================================================
ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator-6/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/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 -: 'dict' and 'dict'

======================================================================
ERROR: test_calculate2 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator-6/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/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 -: 'dict' and 'dict'

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

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator-6/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 numbers"

----------------------------------------------------------------------
Ran 3 tests in 0.002s

FAILED (failures=1, errors=2)

Your code so far

This is the code I have written in mean_var_std.py

import numpy as np

def calculate(list):
  if len(list)!=9:
      raise ValueError('List must contain nine numbers') 
  lst=np.array(list).reshape(3,3)
  calculations={}
  calculations['mean']=[np.mean(lst,axis=0).tolist(),np.mean(lst,axis=1).tolist(),np.mean(lst).tolist()]
  calculations['variance']=[np.var(lst,axis=0).tolist(),np.var(lst,axis=1).tolist(),np.var(lst).tolist()]
  calculations['standard deviation']=[np.std(lst,axis=0).tolist(),np.std(lst,axis=1).tolist(),np.std(lst).tolist()]
  calculations['maximum']=[np.max(lst,axis=0).tolist(),np.max(lst,axis=1).tolist(),np.max(lst).tolist()]
  calculations['minimum']=[np.min(lst,axis=0).tolist(),np.min(lst,axis=1).tolist(),np.min(lst).tolist()]
  calculations['sum']=[np.sum(lst,axis=0).tolist(),np.sum(lst,axis=1).tolist(),np.sum(lst).tolist()]
  return calculations    

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

Your browser information:

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

Challenge Information:

Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

The last error is a straightforward typo, I think you can see the difference here.

The other errors just indicate that your output is not as expected. I would check the results of the example given in the problem and make sure all the numbers match the example answer. If one of those doesn’t match it will show you where the problem is.

In fact, it might be all typos

'max': [[6, 7, 8], [2, 5, 8], 8],
'maximum': [[6, 7, 8], [2, 5, 8], 8], 
1 Like

That worked fine. Thanks!

1 Like

I am also getting the same errors but unable to find the problem:

{‘mean’: ([[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0],), ‘variance’: ([[6.0, 6.0, 6.0], [0.6666666666666666, 0.6666666666666666, 0.6666666666666666], 6.666666666666667],), ‘standard deviation’: ([[2.449489742783178, 2.449489742783178, 2.449489742783178], [0.816496580927726, 0.816496580927726, 0.816496580927726], 2.581988897471611],), ‘max’: ([[6, 7, 8], [2, 5, 8], 8],), ‘min’: ([[0, 1, 2], [0, 3, 6], 0],), ‘sum’: [[9, 12, 15], [3, 12, 21], 36]}
EE.

ERROR: test_calculate (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/bhartigangwani70-mvsdeviation-calculator-5/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/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/unittest/case.py”, line 876, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: ‘dict’ and ‘dict’

======================================================================
ERROR: test_calculate2 (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/bhartigangwani70-mvsdeviation-calculator-5/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/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/unittest/case.py”, line 876, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: ‘dict’ and ‘dict’


Ran 3 tests in 0.007s

FAILED (errors=2)

If you have a question about a specific challenge as it relates to your written code for that challenge need some help, click the Ask for Help button. This button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.