Mean-Std_Variance Assignment - Error

Tell us what’s happening:
Hi,
I am very new to freecodecamp and have begun with Data Analysis course. I have looked up in the forum and based on which I have re-written then below code. Each time I run the program there is this error that comes up saying mean is local variable referenced before assignment? Obviously; I can’t submit my Project before this error is resolved. Please assist.

Your code so far
import numpy as np

def calculate(list):

if len(list) < 9 or len(list) > 9:
    print("List must contain only 9 numbers altogether ")
else:
    arraylist = np.array(list)
    arraylist = arraylist.reshape(3,3)
    
    mean = [(np.mean(arraylist, axis=0).tolist(), np.mean(arraylist, axis =1).tolist(), np.mean(arraylist))]
    var = [(np.var(arraylist, axis=0).tolist(), np.var(arraylist, axis =1).tolist(), np.var(arraylist))]
    stddev = [(np.std(arraylist, axis=0).tolist(), np.std(arraylist, axis =1).tolist(), np.std(arraylist))]
    maxval = [(np.max(arraylist, axis=0).tolist(), np.max(arraylist, axis =1).tolist(), np.max(arraylist))]
    minval = [(np.min(arraylist, axis=0).tolist(), np.min(arraylist, axis =1).tolist(), np.min(arraylist))]
    sumval = [(np.sum(arraylist, axis=0).tolist(), np.sum(arraylist, axis =1).tolist(), np.sum(arraylist))]
    
calculations = { 'Mean': mean,
                'Variance': var,
                'Standard Deviation': stddev,
                'Max': maxval,
                'Min': minval,
                'Sum': sumval
               }

return  calculations

Your browser information:

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

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

  1. in the dict, the ‘Mean’ should be ‘mean’, all lower case.
mean = [(np.mean(arraylist, axis=0).tolist(), np.mean(arraylist, axis =1).tolist(), np.mean(arraylist))]
    var = [(np.var(arraylist, axis=0).tolist(), np.var(arraylist, axis =1).tolist(), np.var(arraylist))]
    stddev = [(np.std(arraylist, axis=0).tolist(), np.std(arraylist, axis =1).tolist(), np.std(arraylist))]
    maxval = [(np.max(arraylist, axis=0).tolist(), np.max(arraylist, axis =1).tolist(), np.max(arraylist))]
    minval = [(np.min(arraylist, axis=0).tolist(), np.min(arraylist, axis =1).tolist(), np.min(arraylist))]
    sumval = [(np.sum(arraylist, axis=0).tolist(), np.sum(arraylist, axis =1).tolist(), np.sum(arraylist))]

remove extra brackets. so it looks something like this.

sumval = [np.sum(arraylist, axis=0).tolist(), np.sum(arraylist, axis =1).tolist(), np.sum(arraylist)]
  1. for the check if the list has a length of 9. Instead of that, put the reshape in a try and except block. this is what is ment by :

"If a list containing less than 9 elements is passed into the function, it should raise a ValueError exception with the message: “List must contain nine numbers.” The values in the returned dictionary should be lists and not Numpy arrays. "

hope this helps

Thank you; finally the error message his different from the earlier one. Although; it’s been well over 35 minutes that I can’t get rid of indentation error as below:

def calculate(list):
    try:
        arraylist = (np.asarray(list)).reshape((3,3))
    except ValueError:
        raise ValueError("List must contain nine numbers.")
    
     mean=....

try and execpt, you dont use if. The indented part after try is the part python trys to execute. if an error occurs(traceback) then the except is executed. otherwise it skips the execpt.

Yes; Try Catch block revised and re-written; couple of indentation errors but appears to be all good now; thank you very much for your assistance SupremeSadat :slight_smile:

The Unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. Python doesn’t have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . To solve this problem, you can explicitly say it’s a global by putting global declaration in your function. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes a variable to global variable everywhere in the function.