madermx
February 19, 2024, 4:56pm
1
Tell us what’s happening:
I get this error
for test_calculate and tes_calculate2:
in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
I don’t understand and my results look identical.
Your code so far
import numpy as np
def calculate(lst):
# The input of the function should be a list containing 9 digits.
cond1 = all(str(element).isdigit() for element in lst)
cond2 = len(lst) == 9
message = 'GO!'
if len(lst) != 9:
raise ValueError("List must contain nine numbers.")
if cond1 and cond2:
# Convert the list to a NumPy array
array_3x3 = np.array(lst)
# Reshape the array to a 3x3 array
array_3x3 = array_3x3.reshape(3, 3)
calculations = {}
# Calculate the means
calculations['mean'] = [np.mean(array_3x3, axis=0).tolist(), np.mean(array_3x3, axis=1).tolist(), np.mean(array_3x3.flatten())]
calculations['variance'] = [np.var(array_3x3, axis = 0).tolist(), np.var(array_3x3, axis = 1).tolist(), np.var(array_3x3.flatten())]
calculations['standard deviation'] = [np.std(array_3x3, axis = 0).tolist(), np.std(array_3x3, axis = 1).tolist(), np.std(array_3x3.flatten())]
calculations['max'] = [np.max(array_3x3, axis = 0).tolist(), np.max(array_3x3, axis = 1).tolist(), np.max(array_3x3.flatten())]
calculations['min'] = [np.max(array_3x3, axis = 0).tolist(), np.min(array_3x3, axis = 1).tolist(), np.min(array_3x3.flatten())]
calculations['sum'] = [np.sum(array_3x3, axis = 0).tolist(), np.sum(array_3x3, axis = 1).tolist(), np.sum(array_3x3.flatten())]
return calculations
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0
Challenge Information:
Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator
Learn to Code — For Free
Can you show your full test output? Including the output that looks identical to the example?
madermx
February 20, 2024, 11:33am
3
Hello, Thanks for your answer. Here is the full output
/home/gitpod/.pyenv/shims/python /workspace/boilerplate-mean-variance-standard-deviation-calculator/main.py
{‘mean’: [[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0], ‘variance’: [[6.0, 6.0, 6.0], [0.6666666666666666, 0.6666666666666666, 0.6666666666666666], 6.666666666666667], ‘standard deviation’: [[2.449489742783178, 2.449489742783178, 2.449489742783178], [0.816496580927726, 0.816496580927726, 0.816496580927726], 2.581988897471611], ‘max’: [[6, 7, 8], [2, 5, 8], 8], ‘min’: [[6, 7, 8], [0, 3, 6], 0], ‘sum’: [[9, 12, 15], [3, 12, 21], 36]}
EE.
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.8.18/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.18/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.002s
FAILED (errors=2)
min is different, yours is first:
{
'min': [[6, 7, 8], [0, 3, 6], 0],
}
# and this is the example from the instructions:
{
'min': [[0, 1, 2], [0, 3, 6], 0],
}
1 Like
system
Closed
August 21, 2024, 12:03pm
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.