Mean-Variance-Standard Deviation Calculator Problem passing test

Hello,
I’m trying my code and it should work but it does not pass the test.

import numpy as np

def calculate(list):
    list2=np.array(list)
    if list2.size == 9:
        a=np.reshape(list2,(3,3))
        calculations= {
            'mean':[a.mean(axis=0), a.mean(axis=1), a.mean()],
            'variance':[a.var(axis=0), a.var(axis=1), a.var()],
            'standard deviation':[a.std(axis=0), a.std(axis=1), a.std()],
            'max':[a.max(axis=0), a.max(axis=1), a.max()],
            'min':[a.min(axis=0), a.min(axis=1), a.min()],
            'sum':[a.sum(axis=0), a.sum(axis=1), a.sum()],
        }
    else:
        calculations= 'List must contain nine numbers.'
    return calculations```

Anyone knows why?

What error are you getting?

Don´t really understand it says:

 python main.py
{'mean': [array([3., 4., 5.]), array([1., 4., 7.]), 4.0], 'variance': [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667], 'standard deviation': [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611], 'max': [array([6, 7, 8]), array([2, 5, 8]), 8], 'min': [array([0, 1, 2]), array([0, 3, 6]), 0], 'sum': [array([ 9, 12, 15]), array([ 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/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 "/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()

======================================================================
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 "/usr/lib/python3.8/unittest/case.py", li
 python main.py
{'mean': [array([3., 4., 5.]), array([1., 4., 7.]), 4.0], 'variance': [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667], 'standard deviation': [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611], 'max': [array([6, 7, 8]), array([2, 5, 8]), 8], 'min': [array([0, 1, 2]), array([0, 3, 6]), 0], 'sum': [array([ 9, 12, 15]), array([ 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/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 "/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()

======================================================================
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 "/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()

======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTest
 python main.py
{'mean': [array([3., 4., 5.]), array([1., 4., 7.]), 4.0], 'variance': [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667], 'standard deviation': [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611], 'max': [array([6, 7, 8]), array([2, 5, 8]), 8], 'min': [array([0, 1, 2]), array([0, 3, 6]), 0], 'sum': [array([ 9, 12, 15]), array([ 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/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 "/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()

======================================================================
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 dif
 python main.py
{'mean': [array([3., 4., 5.]), array([1., 4., 7.]), 4.0], 'variance': [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667], 'standard deviation': [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611], 'max': [array([6, 7, 8]), array([2, 5, 8]), 8], 'min': [array([0, 1, 2]), array([0, 3, 6]), 0], 'sum': [array([ 9, 12, 15]), array([ 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/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 "/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()

======================================================================
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 dif
 python main.py
{'mean': [array([3., 4., 5.]), array([1., 4., 7.]), 4.0], 'variance': [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667], 'standard deviation': [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611], 'max': [array([6, 7, 8]), array([2, 5, 8]), 8], 'min': [array([0, 1, 2]), array([0, 3, 6]), 0], 'sum': [array([ 9, 12, 15]), array([ 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/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 "/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()

======================================================================
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 "/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()

======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTest
 python main.py
{'mean': [array([3., 4., 5.]), array([1., 4., 7.]), 4.0], 'variance': [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667], 'standard deviation': [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611], 'max': [array([6, 7, 8]), array([2, 5, 8]), 8], 'min': [array([0, 1, 2]), array([0, 3, 6]), 0], 'sum': [array([ 9, 12, 15]), array([ 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/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 "/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()

======================================================================
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 "/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()

======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTests)
 python main.py
{'mean': [array([3., 4., 5.]), array([1., 4., 7.]), 4.0], 'variance': [array([6., 6., 6.]), array([0.66666667, 0.66666667, 0.66666667]), 6.666666666666667], 'standard deviation': [array([2.44948974, 2.44948974, 2.44948974]), array([0.81649658, 0.81649658, 0.81649658]), 2.581988897471611], 'max': [array([6, 7, 8]), array([2, 5, 8]), 8], 'min': [array([0, 1, 2]), array([0, 3, 6]), 0], 'sum': [array([ 9, 12, 15]), array([ 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/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 "/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()

======================================================================
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 "/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()

======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTests)
----------------------------------------------------------------------
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: ValueError not raised by calculate

----------------------------------------------------------------------
Ran 3 tests in 0.004s

FAILED (failures=1, errors=2)

Hi fkrell,
I didn’t try to reproduce the error, but from looking at your code and output, here’s something you could try:
If you look at the second line of the output, you can see that the lists in your dictionary contain numpy arrays [array([3., 4., 5.]), ...] and not pure lists of lists [[3., 4., 5.] , ...] . You can see the expected format in the file test_module.py .

The other thing is that the assignment asks for a very specific type of error in case there are less than nine values:

[…] it should raise a ValueError exception […]

You are currently returning a string, rather than actually raising an error.

Good luck!

1 Like

Thank you very much, that was it.
If anyone is interested if you use .tolist() you transform numpy array to list. And the ValueError is in google.

may I see your final code?

Sure it was:

import numpy as np

def calculate(list):
list2=np.array(list)
if list2.size == 9:
a=np.reshape(list2,(3,3))
calculations= {
‘mean’:[a.mean(axis=0).tolist(), a.mean(axis=1).tolist(), a.mean().tolist()],
‘variance’:[a.var(axis=0).tolist(), a.var(axis=1).tolist(), a.var().tolist()],
‘standard deviation’:[a.std(axis=0).tolist(), a.std(axis=1).tolist(), a.std().tolist()],
‘max’:[a.max(axis=0).tolist(), a.max(axis=1).tolist(), a.max().tolist()],
‘min’:[a.min(axis=0).tolist(), a.min(axis=1).tolist(), a.min().tolist()],
‘sum’:[a.sum(axis=0).tolist(), a.sum(axis=1).tolist(), a.sum().tolist()],
}
else:
raise ValueError(“List must contain nine numbers.”)
return calculations

Thanks a lot @almut77 and @fkrell. I had the same issue. To me it looked like precision thing: Values in my dictionary had fewer decimal places than the solution. Converting them from np.array to list using .tolist() solved the issue.

@almut77 thanks for your tip!

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