Type error using unit test

Tell us what’s happening:
Unit test are failing and raising this type error:

TypeError: unsupported operand type(s) for -: 'dict' and 'dict'

but I’m supposed to return a dict from the function. I’ve checked the version and is 3.8, is supposed to handle dicts using and ‘assertAlmostEqual’ function. Anybody else with this problem?

Your code so far

import numpy as np

def calculate(list):
  try:
    data = np.array(list)
    matrix = data.reshape(3,3)
  
  except (ValueError):
    raise ValueError("List must contain nine numbers.")
    
  else:
    calculations = {}
calculations['mean'] = [np.mean(matrix, axis = 1).tolist(),
                        np.mean(matrix, axis = 0).tolist(),
                        np.mean(matrix).tolist()]

calculations['variance'] = [np.var(matrix, axis = 1).tolist(),
                            np.var(matrix, axis = 0).tolist(),
                            np.var(matrix).tolist()]

calculations['standard deviation'] = [np.std(matrix, axis = 1).tolist(),
                                      np.std(matrix, axis = 0).tolist(),
                                      np.std(matrix).tolist()]

calculations['max'] = [np.amax(matrix, axis = 1).tolist(),
                        np.amax(matrix, axis = 0).tolist(),
                        np.amax(matrix).tolist()]

calculations['min'] = [np.amin(matrix, axis = 1).tolist(),
                       np.amin(matrix, axis = 0).tolist(),
                       np.amin(matrix).tolist()]

calculations['sum'] = [np.sum(matrix, axis = 1).tolist(),
                       np.sum(matrix, axis = 0).tolist(),
                       np.sum(matrix).tolist()]

return calculations

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:

assertAlmostEqual is able to check if two dictionaries are equal. Otherwise it tries to use subtraction to determine if they are almost equal, but because subtraction is not supported by dictionaries it results in TypeError.

In other words - this error means just that values in dictionary are different than those expected by test.

1 Like

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