NumPy mean, varience project FCC tests

Tell us what’s happening:
Cannot pass last test - catching ValueError
If I remove below raise statement with -------- return-----test fails ‘ValueError not raised’
raise ValueError("List must contain nine numbers.")
first 2 test passes.

If I include raise expression error is

`Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator/mean_var_std.py”, line 7, in calculate
inputlist = np.reshape(inputlist, (3, 3))
File “<array_function internals>”, line 5, in reshape
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/numpy/core/fromnumeric.py”, line 298, in reshape
return _wrapfunc(a, ‘reshape’, newshape, order=order)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/numpy/core/fromnumeric.py”, line 54, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/numpy/core/fromnumeric.py”, line 43, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
ValueError: cannot reshape array of size 7 into shape (3,3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “main.py”, line 5, in
print(mean_var_std.calculate([2,6,2,8,4,0,1,]))
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator/mean_var_std.py”, line 9, in calculate
raise ValueError(“List must contain nine numbers.”)
ValueError: List must contain nine numbers.
exit status 1`

Your code so far

import numpy as np

def calculate(inputlist):
    calculations = dict()
  
  try:
      inputlist = np.reshape(inputlist, (3, 3))
  except ValueError:
      raise ValueError("List must contain nine numbers.")

  mean = [ 
    [float(a) for a in np.mean(inputlist, axis=0)],
    [float(a) for a in np.mean(inputlist, axis=1)],
    float(np.mean(inputlist))
    ]

  variance = [ 
    [float(a) for a in np.var(inputlist, axis=0)],
    [float(a) for a in np.var(inputlist, axis=1)],
    float(np.var(inputlist))
    ]

  sd = [ 
    [float(a) for a in np.nanstd(inputlist, axis=0)],
    [float(a) for a in np.nanstd(inputlist, axis=1)],
    float(np.nanstd(inputlist))
    ]

  max_of_list = [ 
    [int(a) for a in np.amax(inputlist, axis=0)],
    [int(a) for a in np.amax(inputlist, axis=1)],
    int(np.amax(inputlist))
    ]

  min_of_list = [ 
    [int(a) for a in np.amin(inputlist, axis=0)],
    [int(a) for a in np.amin(inputlist, axis=1)],
    int(np.amin(inputlist))
    ]

  sum_of_list = [ 
    [int(a) for a in inputlist.sum(axis=0)],
    [int(a) for a in inputlist.sum(axis=1)],
    inputlist.sum()
    ]

  calculations = {
    'mean': mean,    
    'variance': variance,
    'standard deviation': sd,
    'max': max_of_list,
    'min': min_of_list,
    'sum': sum_of_list
  }

  return calculations

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

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