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

Tell us what’s happening:
hello , my code is working with any list of 9 numbers including those in the test modules but the 3rd test always give me an error(when I enter a list that is not 9 digits) so what’s wrong with my code?

Your code so far

import numpy as np


def calculate(lst):
    dik = {}
    lol = ['mean', 'variance', 'standard deviation', 'max', 'min', 'sum']
    k = np.array(lst)
    try:
        a = k.reshape((3, 3))
    except ValueError:
        return 'List must contain nine numbers.'
    b = np.array([a.mean(axis=0).tolist(), a.mean(
        axis=1).tolist(), a.mean()]).tolist()
    c = np.array([a.var(axis=0).tolist(), a.var(
        axis=1).tolist(), a.var()]).tolist()
    d = np.array([a.std(axis=0).tolist(), a.std(
        axis=1).tolist(), a.std()]).tolist()
    e = np.array([a.min(axis=0).tolist(), a.min(
        axis=1).tolist(), a.min()]).tolist()
    f = np.array([a.max(axis=0).tolist(), a.max(
        axis=1).tolist(), a.max()]).tolist()
    g = np.array([a.sum(axis=0).tolist(), a.sum(
        axis=1).tolist(), a.sum()]).tolist()
    bob = [b, c, d, f, e, g]
    for (i, j) in zip(lol, bob):
        dik[i] = j
    return dik

Your browser information:

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

Challenge: Data Analysis with Python Projects - Mean-Variance-Standard Deviation Calculator

Link to the challenge:

this is the error i get

Oh dear, that is very hard to read with those cryptic variable names.

b gets the mean list
c gets the variance list
d gets the standard deviation list
f gets the max list
e gets the min list
g gets the sum list

Yeah, those are bad names.

I think the issue is that you don’t raise an error.

it raises an error whenever i enter a list not equal to 9 digits

No, it returns a string. That’s not the same thing.

… yes… like I said, returning a string is different than raising an exception.

Capture
this doesn’t raise an error?

No, it catches the error and prevents the exception from being raised.

https://docs.python.org/3/reference/simple_stmts.html#raise

ah ok i replaced try clause with raise ValueError(‘message’) if the length of list is less than 9
thanks

1 Like