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

When I run, it shown:
TypeError: unsupported operand type(s) for -: ‘dict’ and ‘dict’
May I ask why? And how to fix it?

Your code so far

import numpy as np

def calculate(list):
    if len(list) < 9:
        raise ValueError ("List must contain nine numbers.")
    else:
        data=np.array(list).reshape(3,3)

        meanaxis1=np.mean(data,axis=0).tolist()
        meanaxis2=np.mean(data,axis=1).tolist()
        meanflattened=np.mean(data).tolist()

        varaxis1=np.var(data,axis=0).tolist()
        varaxis2=np.var(data,axis=1).tolist()
        varflattened=np.var(data).flatten().tolist()

        stdaxis1=np.std(data,axis=0).tolist()
        stdaxis2=np.std(data,axis=1).tolist()
        stdflattened=np.std(data).flatten().tolist()

        maxaxis1=np.max(data,axis=0).tolist()
        maxaxis2=np.max(data,axis=1).tolist()
        maxflattened=np.max(data).flatten().tolist()

        minaxis1=np.min(data,axis=0).tolist()
        minaxis2=np.min(data,axis=1).tolist()
        minflattened=np.min(data).flatten().tolist()

        sumaxis1=np.sum(data,axis=0).tolist()
        sumaxis2=np.sum(data,axis=1).tolist()
        sumflattened=np.sum(data).flatten().tolist()

        calculations={
                      'mean': [meanaxis1, meanaxis2, meanflattened],
                      'variance': [varaxis1, varaxis2, varflattened],
                      'standard deviation': [stdaxis1, stdaxis2, stdflattened],
                      'max': [maxaxis1, maxaxis2, maxflattened],
                      'min': [minaxis1, minaxis2, minflattened],
                      'sum': [sumaxis1, sumaxis2, sumflattened]
                      }
    return calculations

Your browser information:

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

Challenge Information:

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

You’re having an error when using .flatten() on scalar values. Since these functions already return single values for flattened arrays, no need to use the .flatten() call. So, try removing .flatten() where it’s not needed. This change should resolve the error and allow your code to work correctly. Let us know if you’ve any questions.


I have the same problem altohugh i didn’t use the .flatten() and this is the error message I’m having each time :face_exhaling: 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’


Ran 3 tests in 0.001s

FAILED (errors=2)
I run my code in Vs COde and Jupyter notebook and it worked correctly and the output is as expected but when trying to do that in Gitpod it’s always the same EError message

Please open a new topic

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.