Metric Dictionary Write a function that calculates the mean, median, variance, standard deviation, minimum and maximum of of list of items. You can assume the given list is contains only numerical entries, and you may use numpy functions to do this.
def dictionary_of_metrics(items):
gauteng = {
'mean': round(np.mean(items),2),
'median': round(np.median(items),2),
'std': round(np.std(items), 2),
'var': round(np.var(items),2),
'min': round(np.min(items),2),
'max': round(np.max(items),2) }
return gauteng
This is the error message I get
NameError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_10816/1906678183.py in ----> 1 dictionary_of_metrics(gauteng)
NameError: name 'gauteng' is not defined
CodePudding user response:
Statements within the same block of code need to be indented at the same level.
The declaration of the dictionary "gauteng" is preceeded with 4 spaces, while the return statement is preceeded with 3 spaces.
CodePudding user response:
Did you create the list of items list called 'gauteng' before calling the function? If not, you have to create the list of items before using it as a function argument. The code would be
def dictionary_of_metrics(items):
gauteng = {
'mean': round(np.mean(items),2),
'median': round(np.median(items),2),
'std': round(np.std(items), 2),
'var': round(np.var(items),2),
'min': round(np.min(items),2),
'max': round(np.max(items),2) }
return gauteng
gauteng = [1,2,3,4,5]
dictionary_of_metrics(gauteng)
The function creates the gauteng
dictionary in it's local scope. This cannot be accessed globally and it cannot be used for as function argument.