Mean-Variance project : TypeError: unsupported operand type(s) for -

My code run well on PyCharm but when i try to run on replit.com, it showed me an error.
It showed me this:

======================================================================
ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator-1/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-1/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'

----------------------------------------------------------------------

This is my code.
ps. Sorry for ugliness of my code.

import numpy as np

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

    x = np.array(list)
    calculations = {
        'mean': [[],[],0],
        'variance': [[],[],0],
        'standard deviation': [[],[],0],
        'max': [[],[],0],
        'min': [[],[],0],
        'sum': [[],[],0]
    }
    matrix = x.reshape(3,3)

    for i in range(3):
        calculations['mean'][0].append(np.mean(matrix[0::, i:i+1]).tolist())
        calculations['variance'][0].append(np.var(matrix[0::, i:i+1]).tolist())
        calculations['standard deviation'][0].append(np.std(matrix[0::, i:i+1]).tolist())
        calculations['max'][0].append(np.max(matrix[0::, i:i+1]).tolist())
        calculations['min'][0].append(np.min(matrix[0::, i:i+1]).tolist())
        calculations['sum'][0].append(np.sum(matrix[0::, i:i+1]).tolist())
    for i in range(3):
        calculations['mean'][1].append(np.mean(matrix[i]).tolist())
        calculations['variance'][1].append(np.var(matrix[i]).tolist())
        calculations['standard deviation'][0].append(np.std(matrix[i]).tolist())
        calculations['max'][1].append(np.max(matrix[i]).tolist())
        calculations['min'][1].append(np.min(matrix[i]).tolist())
        calculations['sum'][1].append(np.sum(matrix[i]).tolist())
    calculations['mean'][2] = np.mean(x).tolist()
    calculations['variance'][2] = np.var(x).tolist()
    calculations['standard deviation'][2] = np.std(x).tolist()
    calculations['max'][2] = np.max(x).tolist()
    calculations['min'][2] = np.min(x).tolist()
    calculations['sum'][2] = np.sum(x).tolist()

    return calculations


Can anyone help me? I try to search in stackoverflow, but i still don’t seethe answer.
.
.
If you enter this post, i just want to say “Thank you” for your kindness.
Good luck.

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

Generally TypeError: unsupported operand type(s) for -: 'dict' and 'dict', means here only that result returned by function is different from the expected by the test.

Could you share link to your code on replit?

print() your calculations and compare them to the expected format.

Also numpy methods actually allow you to calculate all columns or rows in one go, instead of the need to write this ton of .append().

One thing that might cause the error is the final set of commands:

This creates a list, but this is supposed to be a float.

This is my code on replit.

Oh… Thanks for your kindness.
Because of your feedback this made me to review my code carefully.
It turns out that I missed one position which is

calculations['standard deviation'][0].append(np.std(matrix[i]).tolist())

This is code that have to return a axis2 but because i put 0 in it, according to my approach, it return the first axis.
So i just have to change the line from this code to:

calculations['standard deviation'][1].append(np.std(matrix[i]).tolist())

Now. I have passed this project.
And yes, this is my mistake.

1 Like

Oh… Thanks for your kindness.
Your feedback make me re-check my code.
Here is my mistake.

calculations['standard deviation'][0].append(np.std(matrix[i]).tolist())

This is code that have to return a axis2 but because i put 0 in it, according to my approach, it return the first axis.
So i just have to change the line from this code to:

calculations['standard deviation'][1].append(np.std(matrix[i]).tolist())

Now. I have passed this project.
And yes, this is my mistake.

I will try to approach this project in other way for testing my knowledge like @Jagaya suggested. Thanks for your kindness again. May you be healthy in this pandemic. Good luck. :blush: :blush: :blush:

1 Like

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