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

Hi there! My english is not very good, but I’ll try to explain my self the best possible. I’m having problems with the ‘Exception’ of the project, when the list has just a few numbers (<9). I’d already try with a lot of alternatives and modifications, but I’ve not see which is(are) the detail(s) that are making mistakes in it.

Please help me! Greetings from Chile!

import numpy as np

def calculate(list):
  try:
    if len(list)==9:
      a=np.array(list)
      b=a.reshape(3,3)
      m1=np.mean(b,axis=0).tolist()
      m2=np.mean(b,axis=1).tolist()
      m3=np.mean(b).tolist()
      mean=[m1,m2,m3]
      v1=np.var(b,axis=0).tolist()
      v2=np.var(b,axis=1).tolist()
      v3=np.var(b).tolist()
      variance=[v1,v2,v3]
      s1=np.std(b,axis=0).tolist()
      s2=np.std(b,axis=1).tolist()
      s3=np.std(b).tolist()
      std=[s1,s2,s3]
      ma1=np.max(b,axis=0).tolist()
      ma2=np.max(b,axis=1).tolist()
      ma3=np.max(b).tolist()
      ma=[ma1,ma2,ma3]
      mi1=np.min(b,axis=0).tolist()
      mi2=np.min(b,axis=1).tolist()
      mi3=np.min(b).tolist()
      mi=[mi1,mi2,mi3]
      su1=np.sum(b,axis=0).tolist()
      su2=np.sum(b,axis=1).tolist()
      su3=np.sum(b).tolist()
      su=[su1,su2,su3]
      calculations={'mean':mean, 'variance':variance, 'standard deviation':std, 'max':ma, 'min':mi, 'sum':su}
  except ValueError:
    print('List must contain nine numbers.')

  return calculations

Your code so far

Your browser information:

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

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

Exceptions in python can be raised with the raise keyword. Such exception (or other exceptions, that are not raised specifically, but are result of come operations ending with error) can later be dealt in two ways - either with catching it or allowing it to propagate. In the second case execution of the code will be stopped with the error. On the other hand, in the first case code it’s kind of keeping a look for exception and when it’s encountered, instead of stopping the execution all together it performs operations defined in the except block.

Function here is doing the first thing - it catches the exception and prints out text.

You have check all the values in the list. At the moment, your only checking that it has nine elements, but not if each one is a number. A function to consider is the map method. In addition, you have to check if each value is a number. Also, look over the test case again and make sure you understand what its asking:

    def test_calculate_with_few_digits(self):
        self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,])

I hope this helps. Good luck!