Mean-Variance-Standard Deviation Calculator - Mean-Variance-Standard Deviation Calculator

Hi. I am getting test error for my project on Mean-Variance-Standard Deviation Calculator. The readme wants me to return "List must contain nine numbers." for ValueError which I have done and verified it by changing the numbers in the main.py. It seems to be working yet I don’t know how to deal with the error. Please help me:

Your code so far

import numpy as np

def calculate(list):
  while True:
    try:
      array = np.array(list)
      matrix = np.reshape(array, (3, 3))
      mean_ax_1 = []
      mean_ax_2 = []
      mean_flat = np.mean(list)
      var_ax_1 = []
      var_ax_2 = []
      var_flat = np.var(list)
      st_dv_ax_1 = []
      st_dv_ax_2 = []
      st_dv_flat = np.std(list)
      max_ax_1 = []
      max_ax_2 = []
      max_flat = np.max(list)
      min_ax_1 = []
      min_ax_2 = []
      min_flat = np.min(list)
      sum_ax_1 = []
      sum_ax_2 = []
      sum_flat = np.sum(list)

      for i in range(matrix[0].size):
        mean_ax_1.append(matrix[:, i].mean())
        var_ax_1.append(matrix[:, i].var())
        st_dv_ax_1.append(matrix[:, i].std())
        max_ax_1.append(matrix[:,i].max())
        min_ax_1.append(matrix[:, i].min())
        sum_ax_1.append(matrix[:, i].sum())

      for i in range(matrix[:,0].size):
        mean_ax_2.append(matrix[i].mean())
        var_ax_2.append(matrix[i].var())
        st_dv_ax_2.append(matrix[i].std())
        max_ax_2.append(matrix[i].max())
        min_ax_2.append(matrix[i].min())
        sum_ax_2.append(matrix[i].sum())

      calculations = {
        'mean': [mean_ax_1, mean_ax_2, mean_flat],
        'variance': [var_ax_1, var_ax_2, var_flat],
        'standard deviation': [st_dv_ax_1, st_dv_ax_2, st_dv_flat],
        'max': [max_ax_1, max_ax_2, max_flat],
        'min': [min_ax_1, min_ax_2, min_flat],
        'sum': [sum_ax_1, sum_ax_2, sum_flat]
      }
      
      return calculations
      break

    except ValueError:
      return "List must contain nine numbers."

output

{'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]}
..F
======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-mean-var-std/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.003s

FAILED (failures=1)
 
KeyboardInterrupt
 
KeyboardInterrupt
 

Your browser information:

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

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

try this:
if len(list) != 9:
raise ValueError('List must contain nine numbers ')

1 Like

Thank you @arushgarg.imu that worked fine