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/”, 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/”, line 298, in reshape
return _wrapfunc(a, ‘reshape’, newshape, order=order)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/numpy/core/”, line 54, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/numpy/core/”, 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 “”, line 5, in
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator/”, 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()
      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)],

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

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

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

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

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

  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.