Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

Tell us what’s happening:

My code gave two errors and I don’t know how to solve them.

ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/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 "/home/gitpod/.pyenv/versions/3.

Your code so far

Your browser information:

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

Challenge Information:

Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

and what is your code?

import numpy as np

def calculate(lista):
    if len(lista) != 9:
        raise ValueError('List must contain nine numbers.') 
    else:
        a = np.array(lista)
        b = a.reshape((3,3))

    mean = [(b.mean(axis=0).tolist()), (b.mean(axis=1).tolist()), b.mean()]
    var = [(b.var(axis=0).tolist()), (b.var(axis=1).tolist()), b.var()]
    std = [(b.std(axis=0).tolist()), (b.std(axis=1).tolist()), b.std()]
    maxi = [(b.max(axis=0).tolist()), (b.max(axis=1).tolist()), b.max()]
    mini = [(b.min(axis=0).tolist()), (b.min(axis=1).tolist()), b.min()]
    suma = [(b.sum(axis=0).tolist()), (b.sum(axis=1).tolist()), b.sum()]


    calculations = {'mean': mean,
            'variance': var,
            'standard devitacion': std,
            'max': maxi,
            'min': mini,
            'sum': suma,}

    print(calculations)

calculate should return a value, not print it


I’ve edited your code 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 (').

But even with return I receive these two problems:

raceback (most recent call last):
  File "/workspace/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 "/home/gitpod/.pyenv/versions/3.8.20/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 "/workspace/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 "/home/gitpod/.pyenv/versions/3.8.20/lib/python3.8/unittest/case.py", line 943, in assertAlmostEqual
    diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'

double check what format your output needs to be

I got it, I wrote “deviation” as “devitacion” XD.