Mean_var_std calculator error

Tell us what’s happening:
Hi can any one tell me how to solve this issue as i am doing a project in codecamp
Describe your issue in detail here.

Your code so far

def calculate(list):
  if((len(list)) != 9):
    raise ValueError("List must contain nine numbers.")

  lst = np.array(list)
  print(type(lst))

  row_mean1= [lst[[0,1,2]].mean(),lst[[3,4,5]].mean(),lst[[6,7,8]].mean()]
 
  col_mean1= ([lst[[0,3,6]].mean(),lst[[1,4,7]].mean(),lst[[2,5,8]].mean()])

  row_variance= [lst[[0,1,2]].var(),lst[[3,4,5]].var(),lst[[6,7,8]].var()]  
  col_variance= ([lst[[0,3,6]].var(),lst[[1,4,7]].var(),lst[[2,5,8]].var()])

  row_stdeviation= ([lst[[0,1,2]].std(),lst[[3,4,5]].std(),lst[[6,7,8]].std()])  
  col_stdeviation= ([lst[[0,3,6]].std(),lst[[1,4,8]].std(),lst[[2,5,7]].std()])

  row_max= ([lst[[0,1,2]].max(),lst[[3,4,5]].max(),lst[[6,7,8]].max()])
  col_max= ([lst[[0,3,6]].max(),lst[[1,4,8]].max(),lst[[2,5,7]].max()])

  row_minimum= ([lst[[0,1,2]].min(),lst[[3,4,5]].min(),lst[[6,7,8]].min()])  
  col_minimum= ([lst[[0,3,6]].min(),lst[[1,4,8]].min(),lst[[2,5,7]].min()])

  row_sum= ([lst[[0,1,2]].sum(),lst[[3,4,5]].sum(),lst[[6,7,8]].sum()])
  col_sum= ([lst[[0,3,6]].sum(),lst[[1,4,8]].sum(),lst[[2,5,7]].sum()])




  return {
    'mean': [col_mean1, row_mean1, lst.mean().tolist()],
    'variance': [col_variance, row_variance, lst.var().tolist()],
    'standard deviation': [col_stdeviation, row_stdeviation, lst.std().tolist()],
    'max': [col_max, row_max, lst.max().tolist()],
    'min': [col_minimum, row_minimum, lst.min().tolist()],
    'sum': [col_sum, row_sum, lst.sum().tolist()]
    }

ERRORS IT IS DISPLAYING AS:
ERROR: test_calculate (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-mean-variance-standard-deviation-calculator-3/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 943, 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-3/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 943, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: ‘dict’ and ‘dict’

Your browser information:

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

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

For a start, numpy has built-in functions to create a matrix and then calculate the values for rows and columns - which makes it much more readable.

Did some testing and I think you got the wrong indices for the second-row are. You have [1,4,8] but it’s actually [1,4,7].

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