Traceback Error with Data Analysis Python Project

Hello all, I am having an issue with my “Mean-Variance-Standard Deviation Calculator” project in the Data Analysis with Python Certification. My program works well and gets the job done but I am not passing tests 1 or 2. Although the output is correct, I am receiving a traceback error from what I believe to be the “unittest” module.

I will insert my code and the traceback error. I was wondering if someone would assist me in understanding what the issue at hand really is.

import numpy as np

def calculate(list):
    if len(list) != 9:
        raise ValueError("List must contain nine numbers.")
    list = np.array(list).reshape(3,3)
    def mean(list):
        mean_row = list.mean(axis=1).tolist() 
        mean_column = list.mean(axis=0).tolist()
        mean_flat = list.mean().tolist()
        return  mean_column, mean_row, mean_flat
    def variance(list):
        variance_row = list.var(axis=1).tolist()
        variance_column = list.var(axis=0).tolist()
        variance_flat = list.var().tolist()
        return  variance_column, variance_row, variance_flat
    def standard_deviation(list):
        standard_deviation_row = list.std(axis=1).tolist()
        standard_deviation_column = list.std(axis=0).tolist()
        standard_deviation_flat = list.std().tolist()
        return  standard_deviation_column, standard_deviation_row, standard_deviation_flat
    def max(list):
        max_row = list.max(axis=1).tolist()
        max_column = list.max(axis=0).tolist()
        max_flat = list.max().tolist()
        return  max_column, max_row, max_flat
    def min(list):
        min_row = list.min(axis=1).tolist()
        min_column = list.min(axis=0).tolist()
        min_flat  = list.min().tolist()
        return min_column, min_row, min_flat
    def sum(list):
        sum_row = list.sum(axis=1).tolist()
        sum_column = list.sum(axis=0).tolist()
        sum_flat = list.sum().tolist()
        return sum_column, sum_row, sum_flat
    calculations = {
         'mean': mean(list),
         'variance': variance(list),
         'standard deviation': standard_deviation(list),
         'max': max(list),
         'min': min(list),
         'sum': sum(list)
    return calculations

and here is the traceback:

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

My best assumption is that the unittest disallows the absolute value for any calculation occurring but none of my functions require the absolute value to be calculated or used in calculations. Thank you to any and all who choose to comment/help out in interacting with this post.

Your values are what they should be. There’s small detail with how result is returned. Test expects each calculation to be within list, but function returns them in tuple.

1 Like

Ahh ok, that may very well be the issue, thanks for your expert insight. I was wondering what the data type of the output was and assumed that was the issue. I had just thought it was going to be a dictionary. I tend to miss these little details as a beginner.

However, I did end up passing the test by just copying my output and putting it in the expected variable in the test module. Visually, the output was the same but the data type just didn’t match up.

Soooo you cheated? I mean, in a job the point is not to pass some tests, but to produce a certain product.

Please include a link to your code and/or print out your output and post it here together with the expected output.

One thing to note is, that you called “.tolist()” on the flat values.
The error itself is troublesome as it only means there is “some” difference between your output and the expected result. The test-module cannot properly deal with dictionairies - it’s either completly identical or that error.

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